【发布时间】:2016-10-07 05:40:31
【问题描述】:
我正在尝试读取 xml 文件,并且在读取时我将 #text 作为值。我已经删除了所有空格,然后这个#text也不断出现。解决方案是什么?
这是我原来的xml文件
<book genre='novel' ISBN='1-861003-78' misc='sale-item'>
<title>The Handmaid's Tale</title>
<price>14.95</price>
</book>
这是我删除空格后的新 xml 文件
<!--sample XML fragment--><book genre='novel' ISBN='1-861003-78' misc='sale-item'><title>The Handmaid's Tale</title><price>14.95</price></book>
我正在尝试验证两个 xml 文件,这是代码
static bool structValidate( XmlNodeList xmlOldNode, XmlNodeList xmlNewNode)
{
if (xmlOldNode.Count != xmlNewNode.Count) return true;
for (var i = 0; i < xmlOldNode.Count; i++)
{
var nodeA = xmlOldNode[i];
var nodeB = xmlNewNode[i];
Console.WriteLine("\n" + nodeA.Name + ":");
Console.WriteLine("\n" + nodeB.Name + ":");
Console.ReadLine();
if (nodeA.Attributes == null )
{
if (nodeB.Attributes != null)
return true;
else
continue;
}
if (nodeA.Attributes.Count != nodeB.Attributes.Count
|| nodeA.Name != nodeB.Name) return true;
for (var j = 0; j < nodeA.Attributes.Count; j++)
{
var attrA = nodeA.Attributes[j];
var attrB = nodeB.Attributes[j];
Console.WriteLine(attrA.Name);
Console.WriteLine(attrB.Name);
Console.ReadLine();
if (attrA.Name != attrB.Name) return true;
}
if (nodeA.HasChildNodes && nodeB.HasChildNodes)
{
return structValidate(nodeA.ChildNodes, nodeB.ChildNodes);
}
else
{
return false;
}
}
return false;
}
所以在打印时我得到#text
【问题讨论】:
-
您的具体要求是什么?您想存储没有空格的 XML 吗?还是想做一些其他的操作?
-
我的具体操作是比较两个xml文件的结构。例如
没有孩子,所以下一次迭代应该是 。但是下一次迭代是 #text -
为什么不创建 XML 模式并使用模式验证 XML?
-
或使用 XMLReader stackoverflow.com/questions/19954062/…
标签: c# xml xml-parsing