如何使用xmlreader获取第一个找到的节点的内容?
How to get the contents of the first found node using xmlreader?
如果我有这样的 xml 文件
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>
如何使用 xmlreader 获取第一个 price 节点的值?
我尝试了下面的代码,但它没有按照我的意愿执行...
XmlReaderSettings settings=new XmlReaderSettings();
settings.DtdProcessing=DtdProcessing.Ignore;
XmlReader reader=XmlReader.Create(@"D:\abc.xml",settings);
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name=="price"))
{
Console.WriteLine(reader.ReadInnerXml().First());
}
}
Console.ReadLine();
我在这里错过了什么?
我还听说 xmlreader 在读取和写入大型 xml 文件方面比 Xdocument 更好,如果我 运行 在具有简单双核 cpu 和 1-2 GB ram 的 PC 上运行程序,它必须有多大才能减慢或崩溃我的程序?
我想使用 foreach 循环读取和修改多个 xml 文件并一个一个打开每个 xml 文件并执行 reading/modification..
您的代码 return 在每个 price
元素的第一个字符中;您想要 return 所有第一个 price
元素然后停止。
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "price"))
{
Console.WriteLine(reader.ReadInnerXml());
break;
}
}
如果我有这样的 xml 文件
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>
如何使用 xmlreader 获取第一个 price 节点的值? 我尝试了下面的代码,但它没有按照我的意愿执行...
XmlReaderSettings settings=new XmlReaderSettings();
settings.DtdProcessing=DtdProcessing.Ignore;
XmlReader reader=XmlReader.Create(@"D:\abc.xml",settings);
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name=="price"))
{
Console.WriteLine(reader.ReadInnerXml().First());
}
}
Console.ReadLine();
我在这里错过了什么?
我还听说 xmlreader 在读取和写入大型 xml 文件方面比 Xdocument 更好,如果我 运行 在具有简单双核 cpu 和 1-2 GB ram 的 PC 上运行程序,它必须有多大才能减慢或崩溃我的程序? 我想使用 foreach 循环读取和修改多个 xml 文件并一个一个打开每个 xml 文件并执行 reading/modification..
您的代码 return 在每个 price
元素的第一个字符中;您想要 return 所有第一个 price
元素然后停止。
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "price"))
{
Console.WriteLine(reader.ReadInnerXml());
break;
}
}