如何遍历具有相同元素的 xml 文档?
How do i traverse through an xml document that has identical elements?
我有以下 xml 文件
<Universe id="123nebula" name="winterfell" parentId="0">
<planet id="gio234" name="bobka grantos">
<planet id="tyu88" name="viola winter" description="dgdgdddgddgdddgd"/>
</planet>
<planet id="huio90" name="bintor nardi" description="dedededddddddddd"/>
<planet id="ruil99" name="torian fartknox" description="llllklklklkkllk"/>
<planet id="huy7777" name="vivalid durol" description="ppppppppppssssss"/>
<planet id="fila7866" name="hella fella dorrrah">
<planet id="asaaa23" name="Sixty two nine pine" description="ffffffffdfdfd"/>
<planet id="tyu88" name="viola winter" description="dgdgdddgddgdddgd"/>
<planet id="juiiko8" name="tae bo" description="jujujioooppoiiu"/>
</planet>
</Universe>
这里有些行星是独立的,有些行星有子行星。由于所有母行星和子行星都具有相同的元素名称 'planet',因此识别母行星的唯一方法是查找 描述 attribute
,其中只有子行星( sub) 行星具有“描述”attribute
.
我想做两件事:
- 我需要得到父行星的所有子行星
id = fila7866
- 我想获取所有没有子行星的行星
更新
这需要使用 LINQ-2-XML!
来完成
我该怎么做?
试试这个...
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Deserialize single instance
XmlSerializer serializerSingle = new XmlSerializer(typeof(Universe));//, new XmlRootAttribute("document"));
using (FileStream stream = File.OpenRead(@"<Path to your XML data>\planet.xml"))
{
// 'ReadXML.Xml2CSharp.Document' is the 'Document' class in your XML Classes
Universe dezerializedXMLSingle = (Universe)serializerSingle.Deserialize(stream);
var SubPlanets = (from p in dezerializedXMLSingle.Planet where p.Id == "fila7866" select p.Planets).ToList();
} // Put a break-point here, then mouse-over dezerializedXMLSingle
}
}
}
[XmlRoot(ElementName="planet")]
public class Planet {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="description")]
public string Description { get; set; }
[XmlElement(ElementName="planet")]
public List<Planet> Planets { get; set; }
}
[XmlRoot(ElementName="Universe")]
public class Universe {
[XmlElement(ElementName="planet")]
public List<Planet> Planet { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="parentId")]
public string ParentId { get; set; }
}
我将你的 XML 存储在一个文件中并反序列化为一个名为 dezerializedXMLSingle 的对象......实际上非常酷,因为如果你在代码中放置一个断点dezerializedXML单行,然后将鼠标悬停并检查 'dezerializedXMLSingle' 对象的数据结构,您将看到每个行星,然后是与这些行星相关联的行星,在 fila7866 的情况下,您将看到3 [sub] 行星....希望对您有所帮助....
我有以下 xml 文件
<Universe id="123nebula" name="winterfell" parentId="0">
<planet id="gio234" name="bobka grantos">
<planet id="tyu88" name="viola winter" description="dgdgdddgddgdddgd"/>
</planet>
<planet id="huio90" name="bintor nardi" description="dedededddddddddd"/>
<planet id="ruil99" name="torian fartknox" description="llllklklklkkllk"/>
<planet id="huy7777" name="vivalid durol" description="ppppppppppssssss"/>
<planet id="fila7866" name="hella fella dorrrah">
<planet id="asaaa23" name="Sixty two nine pine" description="ffffffffdfdfd"/>
<planet id="tyu88" name="viola winter" description="dgdgdddgddgdddgd"/>
<planet id="juiiko8" name="tae bo" description="jujujioooppoiiu"/>
</planet>
</Universe>
这里有些行星是独立的,有些行星有子行星。由于所有母行星和子行星都具有相同的元素名称 'planet',因此识别母行星的唯一方法是查找 描述 attribute
,其中只有子行星( sub) 行星具有“描述”attribute
.
我想做两件事:
- 我需要得到父行星的所有子行星
id = fila7866
- 我想获取所有没有子行星的行星
更新 这需要使用 LINQ-2-XML!
来完成我该怎么做?
试试这个...
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Deserialize single instance
XmlSerializer serializerSingle = new XmlSerializer(typeof(Universe));//, new XmlRootAttribute("document"));
using (FileStream stream = File.OpenRead(@"<Path to your XML data>\planet.xml"))
{
// 'ReadXML.Xml2CSharp.Document' is the 'Document' class in your XML Classes
Universe dezerializedXMLSingle = (Universe)serializerSingle.Deserialize(stream);
var SubPlanets = (from p in dezerializedXMLSingle.Planet where p.Id == "fila7866" select p.Planets).ToList();
} // Put a break-point here, then mouse-over dezerializedXMLSingle
}
}
}
[XmlRoot(ElementName="planet")]
public class Planet {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="description")]
public string Description { get; set; }
[XmlElement(ElementName="planet")]
public List<Planet> Planets { get; set; }
}
[XmlRoot(ElementName="Universe")]
public class Universe {
[XmlElement(ElementName="planet")]
public List<Planet> Planet { get; set; }
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="parentId")]
public string ParentId { get; set; }
}
我将你的 XML 存储在一个文件中并反序列化为一个名为 dezerializedXMLSingle 的对象......实际上非常酷,因为如果你在代码中放置一个断点dezerializedXML单行,然后将鼠标悬停并检查 'dezerializedXMLSingle' 对象的数据结构,您将看到每个行星,然后是与这些行星相关联的行星,在 fila7866 的情况下,您将看到3 [sub] 行星....希望对您有所帮助....