简体   繁体   English

XML遍历使用XmlDocument

[英]XML traversing using XmlDocument

I have the following code, which I use to traverse the XML: 我有以下代码,我用它来遍历XML:

private void btn_readXML_Click(object sender, EventArgs e)
{
        var doc = new XmlDocument();
        doc.Load("e:\\contacts.xml");
        // Load xml document.            
        TraverseNodes(doc.ChildNodes);     
}

static List<string> xmlnodes = new List<string>();
private static void TraverseNodes(XmlNodeList nodes)
{     
       foreach (XmlNode node in nodes)
       {                   
              List<string> temp = new List<string>();                  
              temp.Add("Node name: " + node.Name.ToString());
              XmlAttributeCollection xmlAttributes = node.Attributes;

              foreach (XmlAttribute at in xmlAttributes)
              {
                   temp.Add("  Atrib: " + at.Name + ": " + at.Value);
              }

               xmlnodes.AddRange(temp);
               TraverseNodes(node.ChildNodes);     
}

But my problem is, I don't want to traverse the whole document, I only want to traverse the node and subsequently its children which has an attribute 'X'. 但我的问题是,我不想遍历整个文档,我只想遍历节点,然后遍历具有属性“X”的子节点。 Please note that I don't know where the node is present. 请注意,我不知道节点在哪里。 So basically what I have to do is, find out if the node exists ( it'll have the attribute 'X'. That's how I identify its the right node) if yes then fetch its children. 所以基本上我要做的是,找出节点是否存在(它将具有属性'X'。这就是我如何识别它的正确节点)如果是,则获取它的子节点。

Can anyone help me out here? 有人可以帮我从这里出去吗? I'm pretty new to XMLs. 我是XML的新手。 Thanks is advance! 谢谢你提前!

Assuming your XML having following structure: 假设您的XML具有以下结构:

<Contacts>
   <Contact X="abc">
       <Child1></Child1>
   </Contact>

   <Contact X="def">
       <Child2></Child2>
   </Contact>
</Contacts>

Example code using XmlNode.SelectNodes : 使用XmlNode.SelectNodes的示例代码:

var doc = new XmlDocument();
doc.Load("e:\\contacts.xml");

//get root element of document   
XmlElement root = doc.DocumentElement;
//select all contact element having attribute X
XmlNodeList nodeList = root.SelectNodes("//Contact[@X]");
//loop through the nodelist
foreach (XmlNode xNode in nodeList)
{       
    //traverse all childs of the node
}

For different XPath Queries see this link . 对于不同的XPath查询,请参阅此链接

UPDATE : 更新

If you want to select all elements having attribute X in the document. 如果要在文档中选择具有属性X所有元素。 No matters where they exists. 没有它们存在的问题。 You could use following: 您可以使用以下内容:

//select all elements in the doucment having attribute X
XmlNodeList nodeList = root.SelectNodes("//*[@X]");

Try this: 试试这个:

private void btn_readXML_Click(object sender, EventArgs e)
{
        var doc = new XmlDocument();
        doc.Load("e:\\contacts.xml");

        var nodes = xdoc.SelectNodes("//yournodename");
        // ex. 
        // var nodes = xdoc.SelectNodes("//Company");
        TraverseNodes(nodes);   
}

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

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