mikemind mikemind - 1 month ago 12
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();
xmlDo.Load("../../liberal.xml");


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
dasjkhf8dfkbhdflak3kjbdf+fafas(safasasdfjgdskalfguv.ng;FHSDAFKLASDF
.

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.

Update1:

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();
dec.Load(retValue);


retValue string has values like this.

<Product><Type>Metal</Type><Department>Foundry</Department></Product>


Error Message

Illegal characters in path.


Really appreciate any help with this.

Answer

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.

Example:

// XmlDocument.LoadXml(string)

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


// XmlDocument.Load(string)

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


For further information, take a look at:

XmlDocument.Load(string)

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).


XmlDocument.LoadXml(string)

String parameter:
String containing the XML document to load.