Convert XmlDocument to String
There aren't any quotes. It's just VS debugger. Try printing to the console or saving to a file and you'll see. As a side note: always dispose disposable objects:
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
xmlDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
return stringWriter.GetStringBuilder().ToString();
}
How to convert XMLDocument type to string in order to show the result in a label
Assuming that your existing code successfully load the XML data to XmlDocument
object, we can then use SelectSingleNode()
passing suitable XPath expression as argument to get any particular part of the XML document. For example, to get <temp_C>
value :
string temp_c = WP_XMLdoc.SelectSingleNode("/data/current_condition/temp_C")
.InnerText;
Another option is using newer XML API, XDocument
. It has Load()
method which functionality is similar to XmlDocument.Load()
:
XDocument WP_XMLdoc = XDocument.Load(WP_Response.GetResponseStream());
Using this approach, we can simply cast XElement
to string
to get it's value :
string temp_c = (string)WP_XMLdoc.XPathSelectElement("/data/current_condition/temp_C");
Converting XMLDocument object to String in Javascript
You can do this by serializing just the root node:
new XMLSerializer().serializeToString(xmlObject.documentElement);
Demo: http://jsfiddle.net/timdown/LmWkL/
Converting XML to string using C#
As Chris suggests, you can do it like this:
public string GetXMLAsString(XmlDocument myxml)
{
return myxml.OuterXml;
}
Or like this:
public string GetXMLAsString(XmlDocument myxml)
{
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
myxml.WriteTo(tx);
string str = sw.ToString();//
return str;
}
and if you really want to create a new XmlDocument
then do this
XmlDocument newxmlDoc= myxml
how to convert xml document object to string?
You will need to serialize your xmlDoc
back to XML once you have made the changes:
var s = new XMLSerializer();
var newXmlStr = s.serializeToString(xmlDoc);
Now you can do what you need to do with the string of updated XML, overwrite your xml
variable, or send it to the server, or whatever...
See the MDN docs for further info
Converting an XML file to string type
Something like this
string xmlString = System.IO.File.ReadAllText(fileName);
Here is good answer to create XmlDocument
XDocument or XMLDocument
XML Document to String
Assuming doc
is your instance of org.w3c.dom.Document
:
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
Convert xml document back to string with jQuery
To help you fix your code in the jsfiddle:
var data = '<value><url>foo</url><name>bar</name></value><value><url>foo</url><name>bar</name></value>'
var xml = $.parseXML('<root>' + data + '</root>');
var $xml = $(xml);
console.log('xml', $xml.find('root').html());
The fix is in the last line: `$xml.find('root').html()
Convert xml document to string with encoding as utf-8
Your custom Utf8StringWriter
class has no constructor accepting a parameter, but you're trying to pass a value to it. That's why you're getting the error message.
Add a public constructor that accepts a StringBuilder
, then pass it on to the base class:
public class Utf8StringWriter : StringWriter
{
public Utf8StringWriter(StringBuilder stringBuilder)
:base(stringBuilder)
{
}
public override Encoding Encoding { get { return Encoding.UTF8; } }
}
Related Topics
Factory Pattern in C#: How to Ensure an Object Instance Can Only Be Created by a Factory Class
What Is the Default Value for Enum Variable
Database.Begintransaction VS Transactions.Transactionscope
Overriding Fields or Properties in Subclasses
Converting an Int[] to Byte[] in C#
Syncing SQL Server 2008 Databases Over Http Using Wcf & Sync Framework
How to Create Byte Array from Httppostedfile
How to Check If a User Belongs to an Ad Group
Comboboxes Are Linked for Some Reason
Wpf Webbrowser Mouse Events Not Working as Expected
Setting Up Swagger (ASP.NET Core) Using the Authorization Headers (Bearer)
How to Test Your Request.Querystring[] Variables
How to Maximize the Browser Window in Selenium Webdriver (Selenium 2) Using C#
Explicitly Freeing Memory in C#