简体   繁体   English

C#读取XML层次结构并从父级使用FailOver

[英]C# Read XML Hierarchy and use FailOver from the parent

I want to create the following XML. 我想创建以下XML。
many kids inside one parent, parent contains kid related nodes (as default for the kids), if the kid is missing a node, the parent will be the failover and give the kis its default node. 一个父级中有许多孩子,父级包含与孩子相关的节点(这是孩子的默认节点),如果孩子缺少节点,则父级将成为故障转移节点,并将kis作为其默认节点。

<parents>
   <parent>
      <!--all parent related nodes will be here-->

      <kid-prop>default kid prop</kid-prop><!--this is the default for all kids-->
      <kids>
         <kid>
            <kid-prop>some data</kid-prop>
         </kid>
         <kid></kid><!--since this kid does not have have <kid-prop>, it will be taken from the parent-->
      <kids>
   <parent>
<parents>

How to do it programatically it is a no-brainer, the question is , is there any annotation way to tell the xmlreader that the parent is the failover of the kids, in case the kid is missing something 如何以编程方式做到这一点不费吹灰之力,问题是,是否有任何注释方法可以告诉xmlreader父级是孩子的故障转移,以防孩子丢失某些东西

No - XmlReader will read you XML as is, there's no concept of "annotations". 否-XmlReader会照原样读取XML,没有“注释”的概念。 If you were using System.Xml.Linq and not XmlReader, it'd be pretty easy: 如果您使用的是System.Xml.Linq而不是XmlReader,那将非常简单:

var defaultValue = (string)root.Element("parent").Element("kid-prop");
root.Element("parent").Element("kids")
.Elements("kid")
.Select(kid => 
       kid.Elements("kid-prop").Any() ? 
       (string)kid.Element("kid-prop") : defaultValue);

By Using XmlReader you can solve the requirement but it needs lot of checks 通过使用XmlReader,您可以解决要求,但需要进行大量检查

Below is the sample code 下面是示例代码

     using (XmlReader reader = XmlReader.Create(@"D:\Sample.xml"))
        {

            bool b = false;
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {

                    if (reader.Name == "parent")
                    {
                        b = true;
                    }

                    if (reader.Name == "kid")
                        b = false;

                    if (!b && reader.Name == "kid-prop")
                    {
                        Console.WriteLine(reader.ReadElementContentAsString());
                    }

                    if (b && reader.Name == "kid-prop")
                    {
                        Console.WriteLine(reader.ReadElementContentAsString());
                    }
                }
            }
        }

Otherwise it is better to use linq to xml as shown in below 否则,最好使用linq到xml,如下所示

        XElement root = XElement.Load(@"D:\Sample.xml");

        var coll = root.Element("parent").Element("kids").Elements("kid").Select(kid => kid.Elements("kid-prop").Any() ? (string)kid.Element("kid-prop") : (string)root.Element("parent").Element("kid-prop"));

        foreach (var item in coll)
        {
            Console.WriteLine(item);
        }

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

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