mikemind mikemind - 3 months ago 30
ASP.NET (C#) Question

load xml file after reading in string

I have read an xml file as a string due to cryptography.

string xmlString = System.IO.File.ReadAllText("../../liberal.xml");
//how to load xml document here?
XmlDocument xmlDo = new XmlDocument();

The above code throws error and doesn't load.

XML file doesn't have any root elements and right now liberal XML file looks like this

Couldn't load xml document with this data format. Only if I can load XML document I will be able to use their properties to add new values to the xml file.


I decrypted the xml and placed in a string, but couldn't load the xml document with that string.

string retValue;
XmlDocument dec = new XmlDocument();

retValue string has values like this.


Error Message

Illegal characters in path.

Really appreciate any help with this.


You're using the XmlDocument.Load(string) method which accepts a path to an XML file. You need to use the XmlDocument.LoadXml(string) method which accepts any valid XML markup.

Two completely different parameters.


// XmlDocument.LoadXml(string)

string decryptedMarkup = "<Product><Type>Metal</Type>"
    + "<Department>Foundry</Department></Product>";
XmlDocument xmlDoc = new XmlDocument();

// XmlDocument.Load(string)

string pathToFile = "test.xml";
XmlDocument xmlDoc2 = new XmlDocument();

For further information, take a look at:


String parameter:
URL for the file containing the XML document to load. The URL can be either a local file or an HTTP URL (a Web address).


String parameter:
String containing the XML document to load.