【发布时间】:2025-12-08 21:35:01
【问题描述】:
如果我有这样的 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 内存的 PC 上运行它? 我想使用 foreach 循环读取和修改多个 xml 文件,并一一打开每个 xml 文件并执行读取/修改..
【问题讨论】:
-
您能否更新您的问题并解释您期望的结果?
-
@FaizanRabbani 结果应该是 39.95 因为它是第一个
price节点的值..