简体   繁体   English

从 XDocument 中的 XML 解析嵌套节点

[英]Parsing Nested Nodes from XML in XDocument

I'm using .net to parse an XML. Below is the XML that needs to be parsed.我用.net解析一个XML,下面是需要解析的XML。 I need to gather the id from Animal and info from AnimalName , AnimalPicture and AnimalPicture type.我需要从Animal收集 id,从AnimalNameAnimalPictureAnimalPicture类型收集信息。

<AnimalEntry version="2.0">
  <Animal id="1">
   <Information>
     <Type>
       Indoor Pet
     <Type>
    <AvailableTypes> //always only 1 type
      <AvailableType> 
         <Active> 
             <AnimalName> Rupert</AnimalName>
             <AnimalPictures>   //Always only 1 picture
                 <AnimalPicture type="jpg"> random.jpg <AnimalPicture> 
            </AnimalPictures>
         </Active>
      </AvailableType>
    </AvailableTypes>
    <Price>10000</Price>
   </Information>
  </Animal>
</AnimalEntry>

I can gather the id as follows:我可以按如下方式收集 id:

            XmlDocument xDoc new XmlDocument();
            xDoc.Load(xmlUrl);

            XmlNode node = xDoc.DocumentElement.ChildNodes[0];
            string id = node.Attributes["id"].Value;

I'm not familiar on how to tackle the nested nodes.我不熟悉如何处理嵌套节点。 Is there a recommended way to get the nested AnimalName, AnimalPicture and OwnerId?是否有推荐的方法来获取嵌套的 AnimalName、AnimalPicture 和 OwnerId? Do I need to call a foreach or is there a way I can directly get the node?我需要调用 foreach 还是有办法直接获取节点?

Use linq to xml.使用 linq 至 xml。

var xml = XElement.Load(xmlUrl);

int id = (int)xml.Element("Animal").Attribute("id");

var active = xml.Element("Animal")
    .Element("Information")
    .Element("AvailableTypes")
    .Element("AvailableType")
    .Element("Active");

var animalName = active.Element("AnimalName").Value;
var animalPictureNode = active.Element("AnimalPictures").Element("AnimalPicture");
var animalPicture = animalPictureNode.Value;
var animalPictureType = animalPictureNode.Attribute("type").Value;

Console.WriteLine(id);
Console.WriteLine(animalName);
Console.WriteLine(animalPicture);
Console.WriteLine(animalPictureType);

Open namespace:打开命名空间:

using System.Xml.Linq;

using linq to xml, u can try something like this.使用 linq 到 xml,你可以尝试这样的事情。 If you this approach verbose, you can also try xpath using linq how-to-query-linq-to-xml-using-xpath如果你这种方法冗长,你也可以尝试 xpath 使用 linq how-to-query-linq-to-xml-using-xpath

        XElement element = XElement.Parse("Xml data");

        foreach (XElement xe in element.Elements(XName.Get("Animal")))
        {
            string id = xe.Attribute(XName.Get("id")).Value;
            var allChildrens = xe.Descendants();

            string animalName = allChildrens.FirstOrDefault(x => x.Name == "AnimalName")?.Value;
            string animalPicture = allChildrens.FirstOrDefault(x => x.Name == "AnimalPicture")?.Value;
            string animalPictureType = allChildrens.FirstOrDefault(x => x.Name == "AnimalPicture")?.Attribute(XName.Get("type"))?.Value;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM