【发布时间】:2014-03-18 20:26:15
【问题描述】:
我有
xmlDoc.OuterXml=
"<template application="test">
<name>ACCOUNT SETUP</name>
<description> ACCOUNT SETUP</description>
<mailFormat>HtmlText</mailFormat>
<message>
<to />
<body>
<p>
<img name="Logo" src="https://www.test.com/00071.gif" />
</p>
</body>
</message>
</template>"
这就是我试图阅读它的方式:
using (XmlReader xmlReader = XmlReader.Create(new System.IO.StringReader(xmlDoc.OuterXml)))
{
while(xmlReader.Read())
{
if(xmlReader.NodeType==XmlNodeType.Element)
{
switch(xmlReader.LocalName)
{
case "name":
Name = xmlReader.ReadString();
break;
case "description":
description = xmlReader.ReadString();
break;
case "to":
to = xmlReader.ReadString();
break;
case "body":
body =FormatSpaces(xmlReader.ReadInnerXml());
break;
}
}
}
}
问题是“body”节点被忽略,而 xmlreader 读取的是“p”节点(位于 body 内部)。如何让 XmlReader 将“body”识别为 XmlNodeType.Element?
【问题讨论】:
-
你反对只使用 LINQ to XML 吗?
-
不,只要结果相同