C# XmlReader-读取子对象
我在使用xmlReader读取字符串(xml结构)时遇到问题C# XmlReader-读取子对象,c#,xml,xmlreader,C#,Xml,Xmlreader,我在使用xmlReader读取字符串(xml结构)时遇到问题 string xml = @" <Data> <Prices Id="16" Code="C3" > <Units> <Unit Id="17"/> </Units> </Prices> <Units> <Unit Id="1" IsActive="true" /> <
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>
"
它工作得很好,但我可以处理非常大的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可以帮助您。在这里您可以找到全文: 简短示例: 使用制度; 使用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。它在所有节点上迭代,直到找到一个单元
元素。然后检查是否存在属性值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();
}
}
}
}
publicstaticvoidmain()
{
字符串xml=”“;
var memoryStream=newmemorystream(Encoding.UTF8.GetBytes(xml));
XmlTextReader=新的XmlTextReader(memoryStream);
while(reader.Read())
{
//继续阅读,直到我们看到一本书的元素
if(读卡器名称等于(“单位”)&&
(reader.NodeType==XmlNodeType.Element))
{
if(reader.GetAttribute(“Id”)=“1”| | reader.GetAttribute(“Id”)=“2”)
{
字符串isActive=reader.GetAttribute(“isActive”);
}
其他的
{
reader.Skip();
}
}
}
}
如果这是一个C#问题,请出示您的代码并解释您的“麻烦”到底是什么。您尝试过什么?您希望输出是什么?对于如此小的XML文档,使用如此低级的API的原因是什么?LINQ转换成XML会让这变得很简单。我更新了帖子。我想要只读的“Units”元素,它是数据的直接子元素
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();
}
}
}
}