tags:

views:

4912

answers:

9

I am creating a lightweight editor in C# and would like to know the best method for converting a string into a nicely formatted XML string. I would hope that there's a public method in the C# library like "public bool FormatAsXml(string text, out string formattedXmlText)", but it couldn't be that easy, could it?

Very specifically, what would the method "SomeMethod" have to be that would produce the output below?

string unformattedXml;
string formattedXml;

unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>"
formattedXml = SomeMethod(unformattedXml);

Console.WriteLine(formattedXml);

Output:

<?xml version="1.0"?>
  <book id="123">
    <author>Lewis, C.S.</author>
    <title>The Four Loves</title>
  </book>
A: 

Is the string valid XML? Do you mean how can you convert an XML string into an XML document? If so, do this:

XmlDocument xml = new XmlDocument();

xml.LoadXml( YourString );
rp
Did you not see the two or three other answers that say the exact same thing?
ck
+4  A: 

It sounds like you want to load the XML into an XmlTextWriter objects and set the Formatting and Indentation properties:

writer.Formatting = Formatting.Indented;
writer.Indentation = 1;
writer.IndentChar = '\t';
defeated
I've used this approach myself in the past (relatively easy), but with .NET 2.0 and later, Microsoft now recommend using the XmlTextWrtierSettings class so that you can take advantage of new features added in 2.0 and 3.5. See the link in my answer.
Ash
+10  A: 

Unfortunately no, it's not as easy as a FormatXMLForOutput method, this is Microsoft were talking about here ;)

Anyway, as of .NET 2.0, the recommended approach is to use the XMlWriterSettingsClass to set up formatting, as opposed to setting properties directly on the XmlTextWriter object. See this MSDN page for more details. It says:

"In the .NET Framework version 2.0 release, the recommended practice is to create XmlWriter instances using the XmlWriter.Create method and the XmlWriterSettings class. This allows you to take full advantage of all the new features introduced in this release. For more information, see Creating XML Writers. "

Here is an example of the recommended approach:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = ("    ");
using (XmlWriter writer = XmlWriter.Create("books.xml", settings))
{
    // Write XML data.
    writer.WriteStartElement("book");
    writer.WriteElementString("price", "19.95");
    writer.WriteEndElement();
    writer.Flush();
}
Ash
+7  A: 

Using the new System.Xml.Linq namespace (System.Xml.Linq Assembly) you can use the following:

string theString = "<nodeName>blah</nodeName>";
XDocument doc = XDocument.Parse(theString);

You can also create a fragment with:

string theString = "<nodeName>blah</nodeName>";
XElement element = XElement.Parse(theString);

If the string is not yet XML, you can do something like this:

string theString = "blah";
//creates <nodeName>blah</nodeName>
XElement element = new XElement(XName.Get("nodeName"), theString);

Something to note in this last example is that XElement will XML Encode the provided string.

I highly recommend the new XLINQ classes. They are lighter weight, and easier to user that most of the existing XmlDocument-related types.

Jason Jackson
A: 

If you just need to escape XML characters the following might be useful:

string myText = "This & that > <> &lt;";
myText = System.Security.SecurityElement.Escape(myText);
sbeskur
A: 

Um, your question doesn't really make sense. Perhaps an example of the given input and output might clarify what you are trying to do.

Kevin
+9  A: 
string unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>";
string formattedXml = XElement.Parse(unformattedXml).ToString();
Console.WriteLine(formattedXml);

Output:

<book>
  <author>Lewis, C.S.</author>
  <title>The Four Loves</title>
</book>

The Xml Declaration isn't output by ToString(), but it is by Save() ...

  XElement.Parse(unformattedXml).Save(@"C:\doc.xml");
  Console.WriteLine(File.ReadAllText(@"C:\doc.xml"));

Output:

<?xml version="1.0" encoding="utf-8"?>
<book>
  <author>Lewis, C.S.</author>
  <title>The Four Loves</title>
</book>
Wonko
+2  A: 

Jason's approach is the simplest. Here's the method:

private static string FormatXmlString(string xmlString)
{
    System.Xml.Linq.XElement element = System.Xml.Linq.XElement.Parse(xmlString);
    return element.ToString();
}
John
A: 

Assuming your're simply wanting to re-format an XML document to put new nodes on new lines and add indenting, then, if you are using .NET 3.5 or above then the best solution is to parse then output with XDocument, somthing like:

string unformattedXml;
string formattedXml;

unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>";
formattedXml = System.Xml.Linq.XDocument.Parse(unformattedXml).ToString();

Console.WriteLine(formattedXml);

Neat hu?

This should then re-format the XML nodes.

To do this with previous versions of the framework requires a lot more legwork as there is no built in functions to re-calculate the whitespace.

In fact, to do it using pre-Linq classes would be:

string unformattedXml;
string formattedXml;

unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(unformattedXml);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Xml.XmlWriter xw = System.Xml.XmlTextWriter.Create(sb, new System.Xml.XmlWriterSettings() { Indent = true });
doc.WriteTo(xw);
xw.Flush();
formattedXml = sb.ToString();
Console.WriteLine(formattedXml);
info_dev