XmlDocumentEx.cpp
1 #include "XmlDocumentEx.h" 2 3 #include <winrt/Windows.Foundation.Collections.h> 4 5 std::wstring XmlDocumentEx::GetFormatedXml() 6 { 7 stream.clear(); 8 Print(FirstChild(), 0); 9 return stream.str(); 10 } 11 12 void XmlDocumentEx::Print(winrt::Windows::Data::Xml::Dom::IXmlNode node, int indentation) 13 { 14 for (int i = 0; i < indentation; i++) 15 { 16 stream << " "; 17 } 18 19 PrintTagWithAttributes(node); 20 if (!node.HasChildNodes()) 21 { 22 stream << L"</" << node.NodeName().c_str() << ">" << std::endl; 23 return; 24 } 25 26 if (node.ChildNodes().Size() == 1 && !node.FirstChild().HasChildNodes()) 27 { 28 stream << node.InnerText().c_str() << L"</" << node.NodeName().c_str() << ">" << std::endl; 29 return; 30 } 31 32 stream << std::endl; 33 auto child = node.FirstChild(); 34 do 35 { 36 Print(child, indentation + 2); 37 } while (child = child.NextSibling()); 38 39 for (int i = 0; i < indentation; i++) 40 { 41 stream << " "; 42 } 43 stream << L"</" << node.NodeName().c_str() << ">" << std::endl; 44 } 45 46 void XmlDocumentEx::PrintTagWithAttributes(winrt::Windows::Data::Xml::Dom::IXmlNode node) 47 { 48 stream << L"<" << node.NodeName().c_str(); 49 for (int i = 0; i < (int)node.Attributes().Size(); i++) 50 { 51 auto attr = node.Attributes().GetAt(i); 52 stream << L" " << attr.NodeName().c_str() << L"='" << attr.InnerText().c_str() << L"'"; 53 } 54 55 stream << L">"; 56 }