26

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?

2
  • 1
    duplicate:stackoverflow.com/questions/2407302/… Commented Jun 26, 2013 at 14:28
  • Voting to close this, as it should have been in the first place, OP had simply confused how to use the typed parameter that was already passed in. Commented Feb 8, 2022 at 7:48

4 Answers 4

62

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
Sign up to request clarification or add additional context in comments.

1 Comment

1 st answer Works Perfectly
33

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;
}

4 Comments

For future readers, in this case there is no need to use a method to expose this functionality at all.
@ChrisSchaller I find this comment a little cryptic - what do you mean; has OuterXml been replaced with something else?
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 ;)
@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 stackoverflow.com/a/2407324/81053
4
   public string GetXMLAsString(XmlDocument myxml)
    {
        using (var stringWriter = new StringWriter())
        {
            using (var xmlTextWriter = XmlWriter.Create(stringWriter))
            {
               myxml.WriteTo(xmlTextWriter);
               return stringWriter.ToString();
            }

        }    
}

1 Comment

+1 for using, the detour with GetStringBuilder() isn't necessary, stringWriter.ToString() will do it
-1

String pathFile = "C:\File.XML"; String readConfiguration = File.ReadAllText(pathFile , Encoding.Unicode);

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.