I have a function as below
public string GetXMLAsString(XmlDocument myxml) { XmlDocument doc = new XmlDocument(); doc.LoadXml(myxml); StringWriter sw = new StringWriter(); XmlTextWriter tx = new XmlTextWriter(sw); doc.WriteTo(tx); string str = sw.ToString();// return str; }
I'm passing an XML to this method from an another method. But in the doc.loadxml()
, the system is expecting a string and since I'm passing an XML, it throws error.
How to solve this issue?
You will need to serialize your xmlDoc back to XML once you have made the changes: var s = new XMLSerializer(); var newXmlStr = s. serializeToString(xmlDoc); Now you can do what you need to do with the string of updated XML, overwrite your xml variable, or send it to the server, or whatever...
Java Code to Convert an XML Document to String For converting the XML Document to String, we will use TransformerFactory , Transformer and DOMSource classes. "</BookStore>"; //Call method to convert XML string content to XML Document object.
As Chris suggests, you can do it like this:
public string GetXMLAsString(XmlDocument myxml) { return myxml.OuterXml; }
Or like this:
public string GetXMLAsString(XmlDocument myxml) { StringWriter sw = new StringWriter(); XmlTextWriter tx = new XmlTextWriter(sw); myxml.WriteTo(tx); string str = sw.ToString();// return str; }
and if you really want to create a new XmlDocument
then do this
XmlDocument newxmlDoc= myxml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With