Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, what is the best method to format a string as XML?

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> 
like image 309
JohnnyM Avatar asked Oct 12 '08 01:10

JohnnyM


People also ask

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

What is '~' in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What is an operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


2 Answers

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> 
like image 145
Wonko Avatar answered Sep 19 '22 20:09

Wonko


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(); } 
like image 24
Ash Avatar answered Sep 21 '22 20:09

Ash