-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpmls.cs
executable file
·107 lines (85 loc) · 2.95 KB
/
Opmls.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Xml;
namespace Spaetzel.FeedDA
{
public static class Opmls
{
public static List<Outline> ParseOpml(string url)
{
XDocument opmlDoc = XDocument.Load(url);
return ParseOpml(opmlDoc);
}
public static List<Outline> ParseOpml(Stream stream)
{
StreamReader reader = new StreamReader(stream);
XDocument doc = XDocument.Load(reader);
return ParseOpml(doc);
}
public static List<Outline> ParseOpml(XDocument opmlDoc)
{
List<Outline> outlines = new List<Outline>();
foreach (var item in opmlDoc.Descendants("outline"))
{
outlines.AddRange(ParseOutline(item));
}
return outlines.ToList();
}
private static IEnumerable<Outline> ParseOutline(XElement item)
{
List<Outline> output = new List<Outline>();
foreach (var subItem in item.Descendants("outline"))
{
output.AddRange(ParseOutline(subItem));
}
if (item.Attribute("xmlUrl") != null)
{
Outline newOutline = new Outline()
{
Title = GetAttributeValue(item.Attribute("title")),
Text = GetAttributeValue(item.Attribute("text")),
Type = GetAttributeValue(item.Attribute("type"))
};
try
{
newOutline.XmlUrl = new Uri(GetAttributeValue(item.Attribute("xmlUrl")));
}
catch (UriFormatException)
{
}
try
{
newOutline.HtmlUrl = new Uri(GetAttributeValue(item.Attribute("htmlUrl")));
}
catch (UriFormatException)
{
newOutline.HtmlUrl = newOutline.XmlUrl;
}
if ( ( newOutline.XmlUrl != null && newOutline.XmlUrl.ToString().Length > 0 ) || ( newOutline.HtmlUrl != null && newOutline.HtmlUrl.ToString().Length > 0 ) )
{
output.Add(newOutline);
}
else
{
// Didn't get a good html or xml value, don't both adding
}
}
return output;
}
public static string GetAttributeValue(XAttribute attribute)
{
if (attribute == null)
{
return "";
}
else
{
return attribute.Value;
}
}
}
}