0

我正在尝试XDocument使用实例解析数据Linq to XML并将其存储在IEnumerable<MyClass>. 我不想从 xml 文档中获取每个元素,所以我使用ElementAt(#index)它来获取值。但是有时我想要的元素不在文档中,然后我得到索引超出范围异常。有没有更好的方法来解析这个文档中的值?这是代码:

XML 文档如下所示:

<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;}
}

链接到 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)
                 };
4

2 回答 2

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']"),
    ...
}
于 2012-08-09T21:37:44.037 回答
1

我通过添加扩展方法解决了这个问题:

public static class Extensions {
  public static string GetValue(this XElement element) {
    return element == null ? null : element.Value;
  }
}

然后使用Attribute()-based 选择您的值:

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)
                 };

该扩展方法可能有一个内置方法,但我不知道。

于 2012-08-09T21:37:17.780 回答