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>
The ' |= ' symbol is the bitwise OR assignment operator.
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 ...
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.
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.
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>
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(); }
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