I have an application that edits an XML file.
The problem is, that after I save my file, some nodes get combined onto one line.
Example: I want to edit child-node B
<root>
<A>
<B>
<C>somedata</C>
...
</B>
</A>
</root>
After edit I get:
<root>
<A>
<B><C>EditedData</C></B>
</A>
</root>
My code Is
public static bool WriteNodeInnerText(List<string> NodesHirarchi, string InnerText, string PathToXml)
{
if (NodesHirarchi.Count == 0) return false;
XmlDocument doc = new XmlDocument();
doc.Load(PathToXml);
doc.PreserveWhitespace = true;
XmlNode xmlNode;
StringBuilder nodesPath = new StringBuilder();
NodesHirarchi.ForEach(x => nodesPath.Append(string.Format("//{0}", x)));
try
{
xmlNode = doc.SelectSingleNode(nodesPath.ToString());
}
catch (System.Xml.XPath.XPathException ex)
{
Console.WriteLine(ex.Data);
return false;
}
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace,
};
using (XmlWriter writer = XmlWriter.Create(PathToXml,settings))
{
xmlNode.InnerText = InnerText;
doc.Save(writer);
}
return true;
}
please Help me solve it + any comment about the code will be appreciated