XPath获取嵌套值

时间:2015-08-11 13:52:13

标签: xml xpath

我有以下XML:

<List><Item><Owner><v>0009501</v><Owner_Valid><v>AC704795228F4A6FA697FC99C7F8F487</v></Owner_Valid></Owner><MaintenenceTermWeeks><v>0</v></MaintenenceTermWeeks></Item></List>

我需要将每个项目作为记录,没有标记和扁平化,如下所示:

档案:

Owner: 0009501
Owner_Valid: AC704795228F4A6FA697FC99C7F8F487
MaintenenceTermWeeks: 0

这对我使用XPath / List / Item

无效

1 个答案:

答案 0 :(得分:1)

以下是XML的C#类表示:

<div>
  <input type="radio" name="A" id="one" />
  <label for="one">One</label>
  <label for="one">
    <button type="button">One</button>
  </label>

  <input type="radio" name="A" id="two" />
  <label for="two">Two</label>
  <label for="two">
    <button type="button">Two</button>
  </label>
</div>

您需要做的下一件事是将XML反序列化为C#对象:

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="Owner_Valid")]
    public class Owner_Valid {
        [XmlElement(ElementName="v")]
        public string V { get; set; }
    }

    [XmlRoot(ElementName="Owner")]
    public class Owner {
        [XmlElement(ElementName="v")]
        public string V { get; set; }
        [XmlElement(ElementName="Owner_Valid")]
        public Owner_Valid Owner_Valid { get; set; }
    }

    [XmlRoot(ElementName="MaintenenceTermWeeks")]
    public class MaintenenceTermWeeks {
        [XmlElement(ElementName="v")]
        public string V { get; set; }
    }

    [XmlRoot(ElementName="Item")]
    public class Item {
        [XmlElement(ElementName="Owner")]
        public Owner Owner { get; set; }
        [XmlElement(ElementName="MaintenenceTermWeeks")]
        public MaintenenceTermWeeks MaintenenceTermWeeks { get; set; }
    }

    [XmlRoot(ElementName="List")]
    public class List {
        [XmlElement(ElementName="Item")]
        public Item Item { get; set; }
    }

}