XMLReader reading XML file based on attribute value

耗尽温柔 提交于 2019-12-10 12:08:06

问题


I am trying to read the following file, I can read the attributes, but I can't go into the specific element (Address in this case) and read its elements based on the attribute of that (Address) element. Shortly I need to distinguish between work and home addresses. I need to do this with XMLReader class. Can you help?

    <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>"

回答1:


Okay, here are some notes to think about. XMLReader in the sense i understand you use it (with no code example) is that you iterate over the document, since the XMLReader is forward-only, and read-only.

Because of this you need to iterate until you find the node you need. In the example below i find the address element labeled "work" and extract that entire node. Then query on this node as you want.

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;
            }
        }
    }
}

Edit

Made an example that not uses LINQ




回答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>

Java:

 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);

                    }
                }

    }



回答3:


Using XPath you can easily write concise expressions to navigate an XML document.

You would do something like

XmlDocument xDoc = new XmlDocument();

xDoc.LoadXml(myXMLString);

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

Then do whatever you want with homeAddress.

Read more here on w3schools on XPath.



来源:https://stackoverflow.com/questions/9518266/xmlreader-reading-xml-file-based-on-attribute-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!