25

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?

Syscall
  • 19,327
  • 10
  • 37
  • 52
shakz
  • 629
  • 3
  • 14
  • 38

3 Answers3

59

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
Community
  • 1
  • 1
Kimtho6
  • 6,154
  • 9
  • 40
  • 56
31

There's a much simpler way to convert your XmlDocument to a string; use the OuterXml property. The OuterXml property returns a string version of the xml.

public string GetXMLAsString(XmlDocument myxml)
{
    return myxml.OuterXml;
}
Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
  • For future readers, in this case there is no need to use a method to expose this functionality at all. – Chris Schaller Feb 08 '22 at 07:49
  • @ChrisSchaller I find this comment a little cryptic - what do you mean; has `OuterXml` been replaced with something else? – Chris Moutray Feb 08 '22 at 10:17
  • I mean what is the point of wrapping this call in the `GetXMLAsString()` method at all, just tell OP to use `XmlDocument.OuterXml`. I know this was answered a long time ago, I'm coming across noob developers referencing this answer as the reason they created a `GetXMLAsString()` method and I keep rejecting their commits ;) – Chris Schaller Feb 08 '22 at 13:29
  • @ChrisSchaller lol oh I see, yes the answer satisfied the original OP's method in the question - perhaps there was a interface or a need for abstraction for unit tests etc. Anyway I'm amazed this one, with the same answer had so many upvotes and still receives them https://stackoverflow.com/a/2407324/81053 – Chris Moutray Feb 08 '22 at 14:56
4
   public string GetXMLAsString(XmlDocument myxml)
    {
        using (var stringWriter = new StringWriter())
        {
            using (var xmlTextWriter = XmlWriter.Create(stringWriter))
            {
               myxml.WriteTo(xmlTextWriter);
               return stringWriter.ToString();
            }

        }    
}
Kimtho6
  • 6,154
  • 9
  • 40
  • 56
danyolgiax
  • 12,798
  • 10
  • 65
  • 116
  • +1 for `using`, the detour with `GetStringBuilder()` isn't necessary, `stringWriter.ToString()` will do it – Andreas May 28 '11 at 11:06