2012-03-01 50 views
0

我想读取下列文件,我可以读取属性,但我不能进入特定元素(在这种情况下的地址)并阅读其基于属性的元素(地址)元素。不久我需要区分工作地址和家庭地址。我需要用XMLReader类来做到这一点。你能帮我吗?XMLReader读取基于属性值的XML文件

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

你能后你有什么到目前为止已经试过? (我假设你正在使用C#) – aweis 2012-03-01 15:02:39

回答

2

好的,这里有一些需要考虑的注意事项。 XMLReader从某种意义上说,我明白你使用它(没有代码示例)是你遍历文档,因为XMLReader是只向前和只读的。

因此,您需要迭代,直到找到所需的节点。在下面的例子中,我找到标有“work”的地址元素并提取整个节点。然后根据需要在此节点上查询。

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

+0

谢谢,我到了地址节点并选择一个工作,我该如何去名称或街道标记值? – Karaman 2012-03-04 16:24:04

+0

嗨,我已经更新了一个访问街道元素的例子的答案。 – aweis 2012-03-04 16:35:02

+0

非常感谢,不幸的是Linq不是一个选项,.NET 2.0中的程序员,有没有其他的选择?提前致谢。 – Karaman 2012-03-06 06:44:03

1

使用XPath您可以轻松编写简洁的表现形式来浏览XML文档的例子。

你会做这样的事情

XmlDocument xDoc = new XmlDocument(); 

xDoc.LoadXml(myXMLString); 

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

然后做任何你想要的homeAddress

在XPath上阅读全文here on w3schools

+0

嗨,感谢您的答案,但我正在尝试使用XMLReader的速度。 – Karaman 2012-03-04 16:24:46

+1

我必须说(“//地址[@标签='工作']”);非常光滑! – Karaman 2012-03-06 14:25:26

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

        } 
       } 

    }