forked from AngleSharp/AngleSharp.Css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSiteMapping.cs
More file actions
51 lines (45 loc) · 1.42 KB
/
SiteMapping.cs
File metadata and controls
51 lines (45 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#nullable disable
namespace AngleSharp.Css.Tests.Mocks
{
using AngleSharp.Dom;
using AngleSharp.Xml;
using AngleSharp.Xml.Dom;
using AngleSharp.Xml.Parser;
using System;
using System.IO;
sealed class SiteMapping
{
private readonly String _fileName;
private readonly IXmlDocument _xml;
public SiteMapping(String fileName)
{
_fileName = fileName;
var parser = new XmlParser();
var content = File.Exists(fileName) ? File.ReadAllText(_fileName) : "<entries></entries>";
_xml = parser.ParseDocument(content);
}
public String this[String url]
{
get
{
var element = _xml.QuerySelector("entry[url='" + url + "']");
return element?.TextContent;
}
}
public Boolean Contains(String url) => this[url] != null;
public void Add(String url, String fileName)
{
if (Contains(url) == false)
{
var element = _xml.CreateElement("entry");
element.SetAttribute("url", url);
element.TextContent = fileName;
_xml.DocumentElement.AppendChild(element);
using (var writer = File.CreateText(_fileName))
{
_xml.ToHtml(writer, XmlMarkupFormatter.Instance);
}
}
}
}
}