【问题标题】:Finding children of a specific parent tag in XML在 XML 中查找特定父标签的子标签
【发布时间】:2019-12-11 09:31:17
【问题描述】:

所以我有一个看起来有点像这样的 XML 文件:

<root>
   <BillingAddress>
      <AddressType>Billing</AddressType>
      <StreetName>SomeStreet</StreetName>
      <HouseNumber>234</HouseNumber>
      <PostCode>6436</PostCode>
      <PostalPlace>TOWN</PostalPlace>
      <CountryCode>CA</CountryCode>
    </BillingAddress>
    <DeliveryAddress>
      <AddressType>Delivery</AddressType>
      <StreetName>Street</StreetName>
      <HouseNumber>666</HouseNumber>
      <PostCode>432</PostCode>
      <PostalPlace>CITY</PostalPlace>
      <CountryCode>CO</CountryCode>
     </DeliveryAddress>
</root>

这个文件有几个具有相同名称的元素,AddressType、StreetName 等。我想要的是只检索在 BillingAddress 父级中找到的值,而不是 DeliveryAddress。我已经尝试过类似XElement streetName = xmlDocument.Descendants("BillingAddress").First(p =&gt; p.Name.LocalName == "StreetName"); 的方法,但这并没有返回任何东西。最好的方法是什么?

【问题讨论】:

  • 这显然是因为Descendant("BillingAddress")root 返回BillingAddress ...显然获取它的元素应该做的事情

标签: c# .net xml


【解决方案1】:
var billingAddress = xmlDocument.Root.Descendants("BillingAddress");
string streetName = billingAddress.Descendants("StreetName").FirstOrDefault()?.Value;

【讨论】:

    【解决方案2】:

    一个完全不同的方法是使用XmlSerializer。为此,您需要定义一个映射您的XML 的类。例如:

    [XmlRoot]
    public class Root
    {
        [XmlElement]
        public Address BillingAddress { get; set; }
        [XmlElement]
        public Address DeliveryAddress { get; set; }
    }
    
    public class Address
    {
        [XmlElement]
        public string AddressType { get; set; }
        [XmlElement]
        public string StreetName{ get; set; }
        [XmlElement]
        public string HouseNumber { get; set; }
        [XmlElement]
        public string PostCode { get; set; }
        [XmlElement]
        public string PostalPlace { get; set; }
        [XmlElement]
        public string CountryCode { get; set; }
    }
    

    您现在可以使用如下代码反序列化 XML

    string xml = "<Root><BillingAddress><AddressType>Billing</AddressType><StreetName>SomeStreet</StreetName><HouseNumber>234</HouseNumber><PostCode>6436</PostCode><PostalPlace>TOWN</PostalPlace><CountryCode>CA</CountryCode></BillingAddress><DeliveryAddress><AddressType>Delivery</AddressType><StreetName>Street</StreetName><HouseNumber>666</HouseNumber><PostCode>432</PostCode><PostalPlace>CITY</PostalPlace><CountryCode>CO</CountryCode></DeliveryAddress></Root>";
    XmlSerializer serializer = new XmlSerializer(typeof(Root));
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    var r = (Root)serializer.Deserialize(new StringReader(xml));
    

    现在您可以这样访问这些值:

    string streetName = r.BillingAddress.StreetName;
    

    你需要两个用法:

    using System.Xml.Serialization;
    using System.IO;
    

    我发现这种方法在处理大型复杂的XML 文件时非常有用。将XML 结构映射到class,然后允许您以(在我看来)更自然的OOP 方式对组件进行编程。

    【讨论】:

      【解决方案3】:

      获取特定 xml 子项的最短路径:

              var document = new XmlDocument();
                  document.Load("example.xml");
      
              XmlNode billingAddress = document["root"]["BillingAddress"];// or document.GetElementsByTagName("BillingAddress")[0]; (previous answer)
      
              var addressType = billingAddress["AddressType"].InnerText;
              var streetName = billingAddress["StreetName"].InnerText;
      

      【讨论】:

      • 这不是 OP 想要的。他们想要获取 BillingAddress 的子元素。
      • OP:“我想要的是只检索在 BillingAddress 父级中找到的值,而不是 DeliveryAddress。”我认为我的代码正是这样做的。
      • 这里的关键字是'inside'。
      猜你喜欢
      • 2020-12-20
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      相关资源
      最近更新 更多