嗨,我有 xmlFile 如下所示。我的问题是我无法遍历所有节点。我尝试过类似的方法。但我认为这对我来说很难。我很困惑
protected void Page_Load(object sender, System.EventArgs e)
{
string xmlFile = Request.PhysicalApplicationPath + @"myxml.xml";
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreWhitespace = true;
using (XmlReader reader = XmlReader.Create(Server.MapPath("myxml.xml"), settings)) {
while (reader.Read()) {
string xmlContent = "";
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Book") {
while (reader.NodeType != XmlNodeType.EndElement)
{
xmlContent += reader.ReadString().ToString();
}
Label1.Text = xmlContent;
}
}
}
}
Myxml 文件是这样的
<books>
<book>
<id>1</id>
<name>Photodex ProShow: Visual QuickStart Guide</name>
<author>Jon Canfield</author>
<price>$29.99</price>
<type>Photoshop</type>
</book>
</books>