我正在尝试使用XDocument
解析来自Linq to XML
实例的数据并将其存储在IEnumerable<MyClass>
中。我不想从xml文档中获取每个元素,因此我使用ElementAt(#index)
来获取值。但是有时我想要的元素不在文档中,然后我得到索引超出范围异常。有没有更好的方法来解析此文档中的值?这是代码:
XML Document看起来像这样:
<someotherelement> /* I dont want the "someotherelement" part either */
<subelement></subelement>
<subelement></subelement>
</someotherelement>
<result>
<doc>
<int name="personid">8394</int> /* I don't want this value so I am skipping it */
<str name="name">James</str>
<str name="address">30 Awesome Lane</str>
<arr name="randomearray"> /* I want to skip this too */
<str name="random1">SomeValue</str>
<str name="random2">SomeValue</str>
<str name="random2">SomeValue</str>
</arr>
<str name="city">Awesome City</str>
<str name="state">CA</str>
<int name="zipcode">84392</int>
<str name="country">USA</str>
<str name="phonenumber">8309933820</str>
<date name="reportdate">2012-07-27T06:01:05.256Z</date>
</doc>
/* This is missing address and country */
<doc>
<int name="personid">10394</int> /* I don't want this value so I am skipping it */
<str name="name">Mathew</str>
<arr name="randomearray"> /* I want to skip this too */
<str name="random1">SomeValue</str>
<str name="random2">SomeValue</str>
<str name="random2">SomeValue</str>
</arr>
<str name="city">Not Awesome City</str>
<str name="state">CA</str>
<str name="zipcode">58439</str>
<str name="phonenumber">8309933820</str>
<date name="reportdate">2012-07-27T06:01:05.256Z</date>
</doc>
</result>
班级:
public class Doc
{
public string Name {get;set;}
public string Address{get;set;}
public string City {get;set;}
public string State {get;set;}
public int ZipCode {get;set;}
public string Country {get;set;}
public string PhoneNumber {get;set;}
public DateTime ReportDate {get;set;}
}
Linq To Xml
string pathToXml = "http://www.foo.com/something";
var doc = XDocument.Load(pathToXml);
IEnumerable<Doc> myDoc = from item in doc.Descendants("doc")
select new Doc
{
Name = item.Element("str").Value,
Address = item.Element("str").ElementsAfterSelf("str").First().Value,
City = item.Element("str").ElementsAfterSelf("str").ElementAt(1).Value,
State = item.Element("str").ElementsAfterSelf("str").ElementAt(2).Value,
ZipCode = Convert.ToInt32(item.Element("int").Value)
Country = item.Element("str").ElementsAfterSelf("str").ElementAt(3).Value,
PhoneNumber = item.Element("str").ElementsAfterSelf("str").ElementAt(4).Value,
ReportDate = Convert.ToDateTime(item.Element("date").Value)
};
答案 0 :(得分:2)
您可能希望使用XPath,它允许您更舒适地指定元素,例如使用其属性的值,这在您的情况下似乎很有用。
结合扩展方法以避免重复陈述:
public static string GetElementValue(this XElement element, string query)
{
var elem = element.XPathSelectElement(query);
return elem == null ? null : elem.Value;
}
一个例子:
select new Doc
{
Name = item.GetElementValue("str[@name='name']"),
...
}
答案 1 :(得分:1)
我通过添加扩展方法来修复此问题:
public static class Extensions {
public static string GetValue(this XElement element) {
return element == null ? null : element.Value;
}
}
然后使用Attribute()
选择您的值:
IEnumerable<Doc> myDoc = from item in doc.Descendants("doc")
select new Doc
{
Name = item.Element("str").Value,
Address = item.Element("str").ElementsAfterSelf("str").First().Value,
City = item.Element("str").ElementsAfterSelf("str").Where(el => (string)el.Attribute("name") == "city").FirstOrDefault().GetValue(),
State = item.Element("str").ElementsAfterSelf("str").Where(el => (string)el.Attribute("name") == "state").FirstOrDefault().GetValue(),
ZipCode = Convert.ToInt32(item.Element("int").Value),
Country = item.Element("str").ElementsAfterSelf("str").Where(el => (string)el.Attribute("name") == "country").FirstOrDefault().GetValue(),
PhoneNumber = item.Element("str").ElementsAfterSelf("str").Where(el => (string)el.Attribute("name") == "phonenumber").FirstOrDefault().GetValue(),
ReportDate = Convert.ToDateTime(item.Element("date").Value)
};
可能有一个内置的扩展方法,但不是我所知道的。