繁体   English   中英

在xmlnode中选择子节点

[英]select childnode in xmlnode

我从 httpGet 得到了这个结果,我想从中提取一些值。

<?xml version="1.0" encoding="UTF-8"?>
<ns4:endpoint xmlns:ns4="identity.ers.ise.cisco.com" xmlns:ers="ers.ise.cisco.com" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" description="Beskriv" id="eed9d9a0-5c8d-11ea-8778- 
9e4294fe2fb6" name="1A:1A:1A:1A:1A:1A">
  <link rel="self" href="https://10.100.10.10:9060/ers/config/endpoint/eed9d9a0-5c8d-11ea-8778- 
    9e4294fe2fb6" type="application/xml" />
  <customAttributes>
   <customAttributes>
     <entry>
        <key>Changed By API</key>
        <value />
     </entry>
     <entry>
        <key>TAG name</key>
        <value>TagNavn</value>
     </entry>
     <entry>
        <key>API Change Date</key>
        <value>2020-03-02</value>
     </entry>
     <entry>
        <key>API Changed By</key>
        <value>Mot</value>
     </entry>
    </customAttributes>
 </customAttributes>
 <groupId>aa13bb40-8bff-11e6-996c-525400b48521</groupId>
<identityStore />
<identityStoreId />
<mac>1A:1A:1A:1A:1A:1A</mac>
<portalUser />
  <profileId />
  <staticGroupAssignment>true</staticGroupAssignment>
 <staticProfileAssignment>false</staticProfileAssignment>
 </ns4:endpoint>

我可以使用 xmlnode.value 提取“名称”和“描述”,但我不确定如何访问我的自定义属性,例如<mac><groupid>它们是否被视为子节点? 任何帮助表示赞赏。

就像(使用System.Xml.Linq命名空间中的XDocument类)一样简单:

// You don't need below line
// XDocument xml = XDocument.Load(@"path to txt file");
var macElement = xml.Descendants().Where(element => element.Name == "mac").FirstOrDefault();
// Similairly for groupid elemnt
var groupidElement = xml.Descendants().Where(element => element.Name == "groupid").FirstOrDefault();

if(macElement != null)
{
  // do something with macElement.Value
}
if(groupidElement != null)
{
  // do something with groupidElement.Value
}

xml.Descendants()调用也是多余的,因此您可以将其存储在变量中,例如:

var descendants = xml.Descendants();

并重复使用它。

暂无
暂无

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

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