I have an XML file like this:
<Something>....</Something>
<Another>....</Another>
<Other>...</Other>
This XML file does not have a root element (I know that this is the wrong XML format).
I need to create or replace (if it already exists) a node in this XML, but I cannot work with XDocumentor XmlDocument, because they need a root element to work, and I cannot add a root element to this XML, because I cannot change more code in the application (Windows Forms application).
What options do I have for this?
Edit 1: Using the @chridam example, I have this method, but it replaces all of the XML. What do i need to change?
public void ReEscribirNodoXML(string pathXml, string nodoName, string nodeContent)
{
if (File.Exists(pathXml))
{
XmlValidatingReader vr = new XmlValidatingReader(pathXml, XmlNodeType.Element, null);
while (vr.Read())
{
Debug.WriteLine("NodeType: {0} NodeName: {1}", vr.NodeType, vr.Name);
}
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineOnAttributes = true;
var writer = XmlWriter.Create(pathXml, settings);
writer.WriteStartElement(nodoName);
writer.WriteRaw(nodeContent);
writer.WriteEndElement();
writer.Flush();
}
else
{
throw new Exception("I#Error");
}
}
source
share