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 a wrong XML format).
I need to create or replace (if it already exists) a node in this XML, but I can't work with XDocument or XmlDocument because they need a root element to work, and I can't add a root element to this XML because I can't change more code in the Application (Windows forms application).
What are my options to perform this?
Edit 1: Using @chridam's sample, I have this method, but it replaces the entire 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");
}
}
.....looks like? Is it multi-line xml data or each element is represented as single line in file?.....are single line but the new element (or maybe is already there and it should be replaced) is a multi-line.