Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何遍历具有相同元素的xml文档?_C#_Xml_Linq_Asp.net Mvc 4_Linq To Xml - Fatal编程技术网

C# 如何遍历具有相同元素的xml文档?

C# 如何遍历具有相同元素的xml文档?,c#,xml,linq,asp.net-mvc-4,linq-to-xml,C#,Xml,Linq,Asp.net Mvc 4,Linq To Xml,我有以下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" n

我有以下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>

在这里,一些行星是由它们自己形成的,而一些行星则有亚行星。由于所有母行星和子行星都具有相同的元素名称“行星”,因此识别母行星的唯一方法是查找description
属性
,其中只有子(子)行星具有“description”
属性

我想做两件事:

  • 我需要得到所有母行星的子行星,它们的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; }
    }
    
    使用System.Collections.Generic;
    使用System.IO;
    使用System.Xml.Serialization;
    使用System.Linq;
    命名空间控制台应用程序1
    {
    班级计划
    {
    静态void Main(字符串[]参数)
    {
    //反序列化单个实例
    XmlSerializer Serializer Single=新的XmlSerializer(typeof(Universe));/,新的XmlRootAttribute(“文档”);
    使用(FileStream-stream=File.OpenRead(@“\planet.xml”))
    {
    //“ReadXML.Xml2CSharp.Document”是XML类中的“Document”类
    Universe DeserializedXmlSingle=(Universe)serializerSingle.Deserialize(流);
    var SubPlanets=(从dezerializedXMLSingle.Planet中的p开始,其中p.Id==“fila7866”选择p.Planets.ToList();
    }//在此处放置一个断点,然后将鼠标悬停在DeserializedXmlSingle上
    }
    }
    }
    [XmlRoot(ElementName=“planet”)]
    公共级行星{
    [XmlAttribute(AttributeName=“id”)]
    公共字符串Id{get;set;}
    [XmlAttribute(AttributeName=“name”)]
    公共字符串名称{get;set;}
    [XmlAttribute(AttributeName=“description”)]
    公共字符串说明{get;set;}
    [xmlement(ElementName=“planet”)]
    公共列表{get;set;}
    }
    [XmlRoot(ElementName=“Universe”)]
    公共类宇宙{
    [xmlement(ElementName=“planet”)]
    公共列表行星{get;set;}
    [XmlAttribute(AttributeName=“id”)]
    公共字符串Id{get;set;}
    [XmlAttribute(AttributeName=“name”)]
    公共字符串名称{get;set;}
    [XmlAttribute(AttributeName=“parentId”)]
    公共字符串ParentId{get;set;}
    }
    

    我将您的XML存储在一个文件中,并反序列化为一个名为dezerializedXMLSingle的对象。。。实际上很酷,因为如果你在代码中的dezerializedXMLSingle行后面放一个断点,然后鼠标移到“dezerializedXMLSingle”对象的数据结构上,你会看到每个行星,然后是与这些行星相关的行星,在fila7866的例子中,你会看到3个[子]行星。。。。希望这能有所帮助……

    我不喜欢看起来太过时,但不管怎样-在发布之前,你到底试过什么?网络上有很多关于如何使用X(ml)文档的信息,我确信即使是遍历这样的树,也至少有很少的点击率。如果您有实际问题,请提供一些代码。@EugenePodskal应该使用LINQ-2-XML完成,我用itI更新了我的帖子。我仍然非常确信,您应该自己尝试解决这个问题。但这是你自己的事…@EugenePodskal我知道如果元素有多种类型和特定的xpath模式,那么如何获取元素,但在本例中,除了根元素之外,所有元素都具有相同的名称。我相信这是独一无二的,但您是否尝试过使用某些xpath,假设这些元素不是独一无二的?我可以向您保证,对于完全限定的xpath,节点的命名方式没有区别。他们都可以有相同的名字——这其实并不重要。它实际上并不从反序列化结构中过滤所需的信息。2.它没有按照OP的要求使用。@EugenePodskal蒙蒂的回答很好,他用这些属性创建了一个对象,但Eugene是正确的,它没有回答我OP中的两个问题,因此我无法给出答案marks@Monty无意冒犯,但如果你关心“OP永远不会为自己学到任何东西”那你可能不该放弃。现在是1。回答不是最优质的帖子2。没有完全回答问题的答案。附言:只需修改您的答案以使用Linq to XML,所花费的时间不会比编写您之前的评论所花费的时间长:)@EugenePodskal请稍等,您在我们对Monty的评论中提到了“回答不是最优质的帖子”。Eugene的高质量帖子通常是由专家发布的,这里并不是每个人都是专家,论坛的整体理念是互相帮助,我已经通过了一些指导方针,通过阅读这些指导方针给我留下了印象,所以希望帖子的作者是中级到专家级的开发者初学者和开发者会发生什么介于两者之间?@blatheringbob你还没有试过靠自己解决这个问题。嗯,你可能已经试过了,但是在帖子里没有任何提示。高质量与成为一名专家无关,但它与我们的生活息息相关。很遗憾,你不想自己解决这个问题。你可能会认为我和其他像我一样是一个漏洞,但我们只希望有受过教育的同事能编码,有一个地方,我们可以问一个问题。