XMLReader基于属性值读取XML文件

时间:2012-03-01 14:46:47

标签: c# xml-parsing

我正在尝试阅读以下文件,我可以读取属性,但我不能进入特定元素(在本例中为Address)并根据该(Address)元素的属性读取其元素。不久我需要区分工作和家庭地址。我需要使用XMLReader类来完成此操作。你能帮忙吗?

    <Address Label="Work">
       <Name>Name1</Name> 
       <Street>PO 1</Street> 
       <City>City1</City> 
       <State>State 1</State> 
    </Address>
    <Address Label="Home">
       <Name>Name2</Name> 
       <Street>PO 2</Street> 
       <City>City2</City> 
       <State>State 2</State>  
    </Address>"

3 个答案:

答案 0 :(得分:2)

好的,这里有一些需要考虑的注意事项。我理解你使用它(没有代码示例)的意义上的XMLReader是你遍历文档,因为XMLReader只是前向的,而且是只读的。

因此,您需要进行迭代,直到找到所需的节点。在下面的示例中,我找到了标有&#34; work&#34;的地址元素。并提取整个节点。然后根据需要在此节点上进行查询。

using (var inFile = new FileStream(path, FileMode.Open))
{
    using (var reader = new XmlTextReader(inFile))
    {
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    if (reader.Name == "Address" && reader.GetAttribute(0) == "Work")
                    {
                        // Create a document, which will contain the address element as the root
                        var doc = new XmlDocument();
                        // Create a reader, which only will read the substree <Address> ... until ... </Address>
                        doc.Load(reader.ReadSubtree());
                        // Use XPath to query the nodes, here the "Name" node
                        var name = doc.SelectSingleNode("//Address/Name");
                        // Print node name and the inner text of the node
                        Console.WriteLine("Node: {0}, Inner text: {1}", name.Name, name.InnerText);
                    }
                    break;
            }
        }
    }
}

修改

做了一个不使用LINQ的例子

答案 1 :(得分:2)

<强> XML:

<Countries>
  <Country name ="ANDORRA">
    <state>Andorra (general)</state>
    <state>Andorra</state>
  </Country>
  <Country name ="United Arab Emirates">
    <state>Abu Z¸aby</state>
    <state>Umm al Qaywayn</state>
  </Country>

<强>爪哇:

 public void datass(string file)
  {

            string file = HttpContext.Current.Server.MapPath("~/App_Data/CS.xml");
                XmlDocument doc = new XmlDocument();
                if (System.IO.File.Exists(file))
                {
                    //Load the XML File
                    doc.Load(file);

                }


                //Get the root element
                XmlElement root = doc.DocumentElement;
                XmlNodeList subroot = root.SelectNodes("Country");

                for (int i = 0; i < subroot.Count; i++)     
                {

                    XmlNode elem = subroot.Item(i);
                    string attrVal = elem.Attributes["name"].Value;
                    Response.Write(attrVal);
                    XmlNodeList sub = elem.SelectNodes("state");
                    for (int j = 0; j < sub.Count; j++)
                    {
                        XmlNode elem1 = sub.Item(j);
                        Response.Write(elem1.InnerText);

                    }
                }

    }

答案 2 :(得分:1)

使用XPath,您可以轻松编写简洁的表达式来导航XML文档。

你会做类似

的事情
XmlDocument xDoc = new XmlDocument();

xDoc.LoadXml(myXMLString);

XmlNode homeAddress = xDoc.SelectSingleNode("//Address[@Label='Work']");

然后用homeAddress做任何你想做的事。

在XPath上阅读更多here on w3schools