简体   繁体   English

使用XmlDocument从子节点读取属性字符串

[英]Reading attribute string from child nodes using XmlDocument

Im having trouble reading attribute strings while using XmlDocument to read thro child nodes. 我在使用XmlDocument读取子节点时无法读取属性字符串。 To be more specific, im trying to read an XML containing a list of running processes: 更具体地说,我试图读取包含正在运行的进程列表的XML:

<Process_List>
  <Processes>
    <ibmpmsvc ID_1="860" />
    <svchost ID_2="8616" />
    <chrome ID_4="4300" />
    <SearchIndexer ID_5="3868" />
    <smss ID_6="416" />
  </processes>
</Process_List>

Each start element presents a running process along with its ID (and some other stuff later on). 每个start元素都会显示一个正在运行的进程及其ID(以及稍后的其他一些内容)。

So Im using XmlDocument to read thro each child node of /Process_List/Processes: 所以我使用XmlDocument来读取/ Process_List / Processes的每个子节点:

XmlNodeList xnList = xml.SelectNodes("/Process_List/Processes");
foreach (XmlNode xn in xnList)
{
    XmlNodeList cxnList = xn.ChildNodes;
    foreach (XmlNode child in cxnList)
    {
        listProc1.Add(child.Name.ToString());
    }
}

Problem is, Im only getting the child's name, (ibmpmsvc, svchost , chrome) and can't figure out how to get its other attributes. 问题是,我只得到孩子的名字,(ibmpmsvc,svchost,chrome),并且无法弄清楚如何获得其他属性。

thanks! 谢谢!

您可以使用child.Attributes

Take a look at the the XMLNode C# Class for an overview of what properties are available to you : http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.aspx 查看XMLNode C#类,了解可用的属性概述: http//msdn.microsoft.com/en-us/library/system.xml.xmlnode.aspx

listProc1.Add(child.Name.ToString());

Will only give you the Name of the element, which is what you are receiving. 只会给你元素的名称,这是你收到的。 You need to also look at the .Value property and .Attributes (which in turn can be Enumerated through). 您还需要查看.Value属性和.Attributes (反过来可以通过枚举)。

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

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