XmlReader - 读子

XmlReader - read child

我在使用 xmlReader 读取字符串(xml 结构)时遇到问题。

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 的元素 "Units"?

实际上我尝试将此字符串解析为 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 =

<Units>
    <Unit Id="1" IsActive="true" />
    <Unit Id="2" IsActive="true" />
  </Units>

XPathReader 可以帮助您。在这里你可以找到完整的文章:https://msdn.microsoft.com/en-us/library/ms950778.aspx 简短示例: 使用系统; 使用 System.Xml; 使用 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);
      }
   }  
}

请参阅下面的示例,了解如何使用 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();
                }
            }
        }
    }