{"id":92,"date":"2008-12-14T19:35:00","date_gmt":"2008-12-14T19:35:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2008\/12\/14\/get-xml-string-from-xelement\/"},"modified":"2008-12-14T19:35:00","modified_gmt":"2008-12-14T19:35:00","slug":"get-xml-string-from-xelement","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2008\/12\/14\/get-xml-string-from-xelement\/","title":{"rendered":"Get XML String from XElement"},"content":{"rendered":"<p>You can use the WriteTo method of the XElement object to an XmlWriter and this can be created in ten different ways like by giving a filename or passing a stream as output or simply giving the StringBuilder to write to:<\/p>\n<pre><span style=\"color:#000000;\"><br \/><\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> <<\/span><span style=\"color:#808080;\">summary><\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> Generates XML string from an XElement<br \/><\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> <\/span><span style=\"color:#808080;\">summary><\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> <<\/span><span style=\"color:#808080;\">param name=\"xml\"><\/span><span style=\"color:#008000;\">XElement source<<\/span><span style=\"color:#808080;\">\/param><\/span><span style=\"color:#808080;\"><br \/><\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\"> GetXmlString(XElement xml)<br \/>{<br \/>    <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> could also be any other stream<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">    StringBuilder sb <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> StringBuilder();<br \/>   <br \/>    <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Initialize a new writer settings<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">    XmlWriterSettings xws <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> XmlWriterSettings();<br \/>    xws.OmitXmlDeclaration <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">true<\/span><span style=\"color:#000000;\">;<br \/>    xws.Indent <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">true<\/span><span style=\"color:#000000;\">;<br \/><br \/>    <\/span><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> (XmlWriter xw <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> XmlWriter.Create(sb, xws))<br \/>    {               <br \/>        <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> the actual writing takes place<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        xml.WriteTo(xw);<br \/>    }<br \/><br \/>    <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\"> sb.ToString();<br \/><br \/>}<br \/>    <\/span><\/pre>\n<p>It is important to know that the XElement already supports ToString which generates the same results as this code, but when you want to get something else, this could be a good start.<br \/><\/br><br \/>See further information in <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.linq.xelement.writeto.aspx\">MSDN<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use the WriteTo method of the XElement object to an XmlWriter and this can be created in ten different ways like by giving a filename or passing a stream as output or simply giving the StringBuilder to write to: \/\/\/ \/\/\/ Generates XML string from an XElement\/\/\/ summary>\/\/\/ XElement sourcepublic string GetXmlString(XElement xml){ &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2008\/12\/14\/get-xml-string-from-xelement\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Get XML String from XElement&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[33,78,29],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":3,"uagb_excerpt":"You can use the WriteTo method of the XElement object to an XmlWriter and this can be created in ten different ways like by giving a filename or passing a stream as output or simply giving the StringBuilder to write to: \/\/\/ \/\/\/ Generates XML string from an XElement\/\/\/ summary>\/\/\/ XElement sourcepublic string GetXmlString(XElement xml){&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/92"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}