[英]How to remove #text from xml file using c#
我正在嘗試讀取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
#text節點是舊XML文件的解析器返回的空白- <title>
和<price>
節點之前的縮進。
錯誤是您加載舊XML文件的方式。 它將空白解析為XML節點。
您的XML解析方式會將這兩個XML文件視為相同的文件:
<book genre='novel' ISBN='1-861003-78' misc='sale-item'>
<title>The Handmaid's Tale</title>
<price>14.95</price>
</book>
<book genre='novel' ISBN='1-861003-78' misc='sale-item'>
someUnformatedText<title>The Handmaid's Tale</title>
someUnformatedText<price>14.95</price>
</book>
這是XmlNode.Name的文檔
節點的限定名稱。 返回的名稱取決於節點的NodeType:
文字-> #text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.