2015-12-21 61 views
1

我在使用xmlReader讀取字符串(xml結構)時遇到了麻煩。XmlReader - 讀取子女

string xml = @" 
<Data> 
    <Prices Id="16" Code="C3" > 
    <Units> 
     <Unit Id="17"/> 
     </Units> 
    </Prices> 
    <Units> 
    <Unit Id="1" IsActive="true" /> 
    <Unit Id="2" IsActive="true" /> 
    </Units> 
<Product Id="16" Code="C3" > 
     <Names> 
     <Name NameVersion="1" Name="C3 " /> 
     </Names> 
     <Units> 
     <Unit Id="16"/> 
     </Units> 
</Product> 
</Data> 
" 

我怎麼能只讀元素「單位」與孩子的ID 1和2?

其實我試着把這個字符串解析成XDocument。

var root = XElement.Parse(xmlFile); 
var units = root.Elements("Units").FirstOrDefault(); 

它工作正常,但我可以使用真正的大XML,我不想解析所有字符串到XDocument。我正在尋找方法,我可以從字符串中加載部分元素。我也試着做到這一點使用XmlTextReader的,但這在我的情況不工作

using (XmlReader xmlTextReader = XmlReader.Create(new StringReader(xmlFile))) 
     { 
      if (xmlTextReader.ReadToFollowing(elementName)) 
      { 
       bool isOnNode = xmlTextReader.ReadToDescendant(elementName); 
       while (isOnNode) 
       { 
        yield return XNode.ReadFrom(xmlTextReader) as XElement; 
        if (!xmlTextReader.IsStartElement(elementName)) 
         isOnNode = xmlTextReader.ReadToNextSibling(elementName); 
       } 
      } 
     } 

預期輸出:的XElement =

​​
+2

如果這是一個C#問題,那麼請您出示的代碼,並解釋究竟是什麼你的「麻煩」是。 –

+2

你有什麼試過的?你期望輸出是什麼?爲這樣一個小XML文檔使用這樣一個低級別的API是什麼原因? LINQ to XML將使這個微不足道。 –

+1

我更新了帖子。我只想讀取Data的直接子對象「Units」元素 – adamo94

回答

1

查看下面的示例,瞭解如何使用XmlReader解析xml。它遍歷所有節點,直到找到Unit元素。然後,它會檢查屬性值Id的存在和解析的IsActive值:

public static void Main() 
    { 
     string xml = "<Data><Units><Unit Id=\"1\" IsActive=\"true\" /><Unit Id=\"2\" IsActive=\"true\" /></Units><Product Id=\"16\" Code=\"C3\" ><Names><Name NameVersion=\"1\" Name=\"C3 \" /></Names><Units><Unit Id=\"16\"/></Units></Product></Data>"; 
     var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)); 
     XmlTextReader reader = new XmlTextReader(memoryStream); 

     while (reader.Read()) 
     { 
      //keep reading until we see a book element 
      if (reader.Name.Equals("Unit") && 
       (reader.NodeType == XmlNodeType.Element)) 
      { 

       if (reader.GetAttribute("Id") == "1" || reader.GetAttribute("Id") == "2") 
       { 
        string isActive = reader.GetAttribute("IsActive"); 
       } 
       else 
       { 
        reader.Skip(); 
       } 
      } 
     } 
    } 
0

XPathReader可以幫助你。在這裏你可以找到完整的文章:https://msdn.microsoft.com/en-us/library/ms950778.aspx 簡短的例子: 使用系統;使用System.Xml的 ; using System.Xml.XPath;使用GotDotNet.XPath的 ;

public class Test{ 
static void Main(string[] args) { 

     try{ 
XPathReader xpr = new XPathReader("books.xml", "//book/title"); 

      while (xpr.ReadUntilMatch()) { 
       Console.WriteLine(xpr.ReadString()); 
      }  
      Console.ReadLine(); 
    }catch(XPathReaderException xpre){ 
     Console.WriteLine("XPath Error: " + xpre); 
     }catch(XmlException xe){ 
     Console.WriteLine("XML Parsing Error: " + xe); 
     }catch(IOException ioe){ 
     Console.WriteLine("File I/O Error: " + ioe); 
     } 
    } 
}