以下是我的 xml 当前外观的示例:
<Records>
<Record>
<Field id='NAMEOFBUSINESS'>
<Value>J's Burgers</Value>
</Field>
<Field id='BUSINESSPHONE'>
<Value>777-888-9999</Value>
</Field>
<Record>
</Records>
但是,我需要它看起来像这样:
<Records>
<Record>
<Field id='NAMEOFBUSINESS'>
<Value>J's Burgers</Value>
</Field>
<Field id='BUSINESSPHONE'>
<Value>777-888-9999</Value>
</Field>
<Record>
</Records>
目前我的代码如下所示:
using (var sr = new StreamReader(filePath, encode))
{
xmlDocument.Load(sr);
}
XmlNodeList nlist = null;
XmlNode root = xmlDocument.DocumentElement;
if (root != null)
{
nlist = root.SelectNodes("//Field");
}
if (nlist == null)
{
return;
}
foreach (XmlElement node in nlist)
{
if (node == null)
{
continue;
}
var value = node.Value;
if (value != null)
{
var newValue = value.Replace("'", "'");
node.Value = newValue;
}
}
using (var xmlWriter = new XmlTextWriter(filePath, encode))
{
xmlWriter.QuoteChar = '\'';
xmlDocument.Save(xmlWriter);
}
所以我需要转义“'”,但只能在撇号所在的值元素内。