简体   繁体   English

System.Xml.Linq.XElement中的查询元素

[英]Query elements in System.Xml.Linq.XElement

C# code: C#代码:

List<XElement> ele = reportHost.hostProperties.ToList();          
foreach (var item in ele)
{
 Console.WriteLine("XML: {0}", item);
}

OUTPUT: OUTPUT:

<HostProperties>
  <tag name="cpe">cpe:/o:linux:linux_kernel</tag>
  <tag name="device">1234567</tag>
  <tag name="FQN">BOB-PC</tag>
  <tag name="Domain">Internal</tag>
 </HostProperties>

How can I expand the code above to extract the values "BOB-PC" and "1234567" for the XML output above? 如何扩展上面的代码以提取上面的XML输出的值“ BOB-PC”和“ 1234567”?

Assuming ele is a collection of <HostProperties> elements. 假设ele<HostProperties>元素的集合。 You can use Linq to Xml to check the value of the name attribute to get the correct values of the element. 您可以使用Linq to Xml来检查name属性的值以获得正确的元素值。 If you are getting null references, you will have to add additional null checks - as I don't know what is required or not in the xml. 如果要获取空引用,则必须添加其他空检查-因为我不知道xml中是否需要什么。

List<XElement> hostProperties = reportHost.hostProperties.ToList(); 

// iterate through each <hostProperties> element
foreach (var hp in hostProperties)
{
    // all of the <tag> elements
    var tags = hp.Element("HostProperties").Elements("tag");

    foreach (var tag in tags)
    {
      // get the attribute with the name of "name"
      var tagName = tag.Attribute("name");

      // check to make sure a <tag> element exists with a "name" attribute
      if (tagName != null)
      {
        if (tagName.Value == "device")
        {
          Console.WriteLine($"device: {tag.Value}");
        }
        else if (tagName.Value == "fqn")
        {
          Console.WriteLine($"fqn: {tag.Value}");
        }
      }

    }
}

暂无
暂无

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

相关问题 对Xdocument的Linq查询返回“System.linq.Enumerable + WhereSelectEnumerableIterator&#39;2 [system.XML.Linq.Xelement,System.String] - Linq query on Xdocument returns "System.linq.Enumerable+WhereSelectEnumerableIterator'2[system.XML.Linq.Xelement,System.String] System.Xml.Linq.XElement&#39;不包含&#39;XPathEvaluate的定义 - System.Xml.Linq.XElement' does not contain a definition for 'XPathEvaluate 扩展System.Xml.Linq.XElement-内部CloneNode - Extending System.Xml.Linq.XElement - internal CloneNode 如何使用XmlWriter将System.Xml.Linq.XElement写入流 - How to write System.Xml.Linq.XElement using XmlWriter to a stream 如何反序列化 System.Xml.Linq.XElement? - How I can deserialize a System.Xml.Linq.XElement? VS2010将System.Xml.XmlElement与System.Xml.Linq.XElement混淆? - VS2010 confuses System.Xml.XmlElement with System.Xml.Linq.XElement? Web服务:无法将类型&#39;System.Xml.Linq.XElement&#39;隐式转换为&#39;System.Xml.XmlElement&#39; - Webservices : Cannot implicitly convert type 'System.Xml.Linq.XElement' to 'System.Xml.XmlElement' 将xml字符串传递给System.Xml.Linq.XElement时,避免使用尖括号将xml转义 - Avoid xml escaping of angle brackets, when passing xml string to System.Xml.Linq.XElement Linq强制转换Xelement错误:无法将类型为'System.Xml.Linq.XElement'的对象强制转换为'System.IConvertible' - Linq cast conversion Xelement error: Unable to cast object of type 'System.Xml.Linq.XElement' to type 'System.IConvertible' 数据绑定:“ System.Xml.linq.XElement”不包含名称为“ colorName”的属性 - DataBinding: 'System.Xml.linq.XElement' does not contain a property with the name 'colorName'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM