【问题标题】:How to check if element exists in this xml c#如何检查此xml c#中是否存在元素
【发布时间】:2013-12-20 18:23:37
【问题描述】:

我在编程时遇到了这个问题。对于每个特定的端口元素,我想检查<script> exixst。 <script><port> 节点元素的子元素。以这个例子为例,这里是图片的链接:

http://i.stack.imgur.com/T2FGr.png

 // Leggi il file xml

        XDocument xml = XDocument.Load(pathFile.Text);



        var hosts = from h in xml.Descendants("address")
                   select new
                   {
                       addr = h.Attribute("addr").Value,
                       addrtype = h.Attribute("addrtype").Value
                   };

        foreach (var host in hosts)
        {
            if (host.addrtype == "ipv4")
            {
                console.Text += host.addr + Environment.NewLine;
                console.Text += host.addrtype + Environment.NewLine;

                var ports = xml.Descendants("port");

                foreach (var port in ports)
                {
                    var script = port.Element("script");

                    var hasScriptElement = script != null;

                    ?? -> how can i get the elem pem key value? thanks!!
                }


            }
        }

【问题讨论】:

  • 克里斯请张贴一些你的代码。没有它很难帮助你
  • 向我们展示一些示例 xml。将我们需要知道的所有文本放在问题中,而不是放在场外的图片中。

标签: c# xml


【解决方案1】:

使用XDocument...

var ports = document.Descendants("port");

foreach (var port in ports) {
    var script = port.Element("script");

    if (script != null)
    {
        var pem = script.Descendants("elem")
            .FirstOrDefault(n => n.Attribute("pem") != null);
    }
}

【讨论】:

  • 嗯,谢谢,我怎样才能得到 elem key="pem" ?价值?谢谢!
  • 是端口有那个属性还是脚本有那个属性?
  • @SteveFenton,见图片。
  • @SteveFenton ehm 是的,脚本还有其他元素,我会得到带有 key="pem" 的元素。非常感谢 1° 的帮助!
  • 我已将其添加到示例中。根据这些示例,您应该能够使用DescendantsElement 和 Linq 获得几乎任何东西。
【解决方案2】:

通用答案:

  • 找到<port,然后找到>
  • 找到</port>
  • 如果between是<script,那么你有脚本

【讨论】:

  • 很难说出你想问或指出什么。你的意思是,我的回答没有回答图片上的问题(如何获得<pem> 值)?还是您的意思是 <script 可以在 cmets 内。或者是什么? =D
【解决方案3】:

也许您应该考虑使用XML Schema (XSD) 并根据它验证您的XML。这应该被视为最佳实践

您可以找到如何validate your XML against an XSD programatically in C# 的指南。

祝你好运!

【讨论】:

    【解决方案4】:

    这是一个控制台应用程序,演示了如何做到这一点:

    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "c:\path\to\file\test.xml";
            var xmlDoc = new XmlDocument();
            xmlDoc.Load(File.OpenRead(fileName));
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("port");
            XmlNodeReader nodeReader;
            foreach (var node in nodeList)
            {
                using(nodeReader = new XmlNodeReader((XmlNode)node))
                {
                    if(nodeReader.ReadToDescendant("script"))
                    {
                        Console.WriteLine("Found script tag");
                    }
                }
            }
            Console.ReadKey();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多