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();
}
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", "");
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/
XML Document to String?
public static String toString(Document doc) {
try {
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(sw));
return sw.toString();
} catch (Exception ex) {
throw new RuntimeException("Error converting to String", ex);
}
}
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
Converting XML doc to String
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(xmldoc1);
transformer.transform(source, result);
String xmlString = sw.toString();
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
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 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
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()
Related Topics
Spring Scheduling Task - Run Only Once
How to Clear Permgen Space Error in Tomcat
Random Number with Probabilities
How to Ignore Pkix Path Building Failed: Sun.Security.Provider.Certpath.Suncertpathbuilderexception
Calling Base Class Overridden Function from Base Class Method
Converting a String to Color in Java
Why Are Wait() and Notify() Declared in Java's Object Class
Rejectedexecutionexception Inside Single Executor Service
Jackson Change JSONignore Dynamically
Changing Java Platform on Which Netbeans Runs
Efficiently Finding the Intersection of a Variable Number of Sets of Strings
Using Ntlm Authentication in Java Applications
Why Do I Need a Functional Interface to Work with Lambdas
Why Functional Interfaces in Java 8 Have One Abstract Method