I have a string, in which there are plenty of XML code. I'd like to write this string to file so that it will be well-formatted (with indentation), like:
<table>
<tr>
<td>
<style1>text</style1>
</td>
</tr>
</table>
instead of
<table><tr><td><style1>text</style1></td></tr></table>
As far as i understand, LINQ is the most easy way to do it, but LINQ presented only in C#/VB, isn't it? The point is i have to use only C++.NET. Is there any solution how to do that in the simplest way?
SOLUTION: so, here is solution for this topic
...
System::Xml::Linq::XDocument^ doc = System::Xml::Linq::XDocument::Parse(article_string);
formatted_string = doc->ToString();
...
I wouldn't use LINQ itself at all. I'd just use an XML API - probably LINQ to XML. Don't be fooled - the "LINQ" in the term really just means it's an API which is designed to work really well with LINQ to Objects. You don't need any language integration to use it though.
Here's a really simple example in C#:
using System;
using System.Xml.Linq;
public class Test
{
public static void Main()
{
// Use XDocument.Load to load from a file
XDocument doc = XDocument.Parse("<table><tr><td><style1>text</style1></td></tr></table>");
doc.Save(Console.Out);
}
}
Output:
<?xml version="1.0" encoding="ibm850"?>
<table>
<tr>
<td>
<style1>text</style1>
</td>
</tr>
</table>
(Ignore the ibm850
encoding - that's just because that's the encoding that Console.Out
declares on my system.)
LINQ to XML basically formats the XML automatically when you save it, unless you tell it not to.
You can do the whole thing in a single statement, if you just want to format a file:
XDocument.Load("input.xml").Save("output.xml");
I'm sure you can convert that C# to C++ :)
See more on this question at Stackoverflow