JSON.NET XML to String - c#

string json = "{"Animal":{"id":"123","verified":true}}"
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
returnXml = doc.ToString();
Why does "ReturnXml" return the following text "System.Xml.XmlDocument" and not the XML output in string format?
http://json.codeplex.com/

To print XML, you need to use InnerXml
doc.InnerXml;

The ToString method of XmlDocument is not set to output a pretty version of the xml contained therein.
You're best bet may be to just convert that XmlDocument to an XDocument, since that supports a ToString method that outputs actual XML:
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
XDocument linqXML = XDocument.Load(new XmlNodeReader(doc));
returnXML = linqXML.ToString();

Related

How to fix the Unicode of xml file

my xml:
<title>1 Introduction</title>
<p>Compositional models in distributional semantics combine word vectors to yield new compositional vectors that represent</p>
after I convert the XML using xdocument or xmldocument or xelement the Unicode will look like this.
this is my code to save the xml.
XDocument xdocc = XDocument.Load(files);
XElement xele = XElement.Load(files);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(files);
xdocc.Save("will.xml");
xele.Save("will2.xml");
xmldoc.Save("will3.xml");

Write string to xml file

I save all attachments from an email:
List<Pop3Content> contentList = Pop3Message.GetAttachedContents(client.GetMessage(i).Contents, c => c.IsAttachment == true);
XmlDocument xml=new XmlDocument();
xml.LoadXml(contentList[0].BodyText);
this
contentList[0].BodyText
Returns the whole Content of an attached XML file, the way I want to have it in my XmlDocument XML
But the method LoadXML expects a path to an actual XML file. How can I write strings into an "internal" XML file?
This method accepts string.
You can do
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");
Read this
I think you need to do this
xml.LoadXml(contentList[0].BodyText.ToString());

Xml within an Xml

I basically want to know how to insert a XmlDocument inside another XmlDocument.
The first XmlDocument will have the basic header and footer tags.
The second XmlDocument will be the body/data tag which must be inserted into the first XmlDocument.
string tableData = null;
using(StringWriter sw = new StringWriter())
{
rightsTable.WriteXml(sw);
tableData = sw.ToString();
}
XmlDocument xmlTable = new XmlDocument();
xmlTable.LoadXml(tableData);
StringBuilder build = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(build, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
writer.WriteStartElement("dataheader");
//need to insert the xmlTable here somehow
writer.WriteEndElement();
}
Is there an easier solution to this?
Use importNode feature in your document parser.
You can use this code based on CreateCDataSection method
// Create an XmlCDataSection from your document
var cdata = xmlTable.CreateCDataSection("<test></test>");
XmlElement root = xmlTable.DocumentElement;
// Append the cdata section to your node
root.AppendChild(cdata);
Link : http://msdn.microsoft.com/fr-fr/library/system.xml.xmldocument.createcdatasection.aspx
I am not sure what you are really looking for but this can show how to merge two xml documents (using Linq2xml)
string xml1 =
#"<xml1>
<header>header1</header>
<footer>footer</footer>
</xml1>";
string xml2 =
#"<xml2>
<body>body</body>
<data>footer</data>
</xml2>";
var xdoc1 = XElement.Parse(xml1);
var xdoc2 = XElement.Parse(xml2);
xdoc1.Descendants().First(d => d.Name == "header").AddAfterSelf(xdoc2.Elements());
var newxml = xdoc1.ToString();
OUTPUT
<xml1>
<header>header1</header>
<body>body</body>
<data>footer</data>
<footer>footer</footer>
</xml1>
You will need to write the inner XML files in CDATA sections.
Use writer.WriteCData for such nodes, passing in the inner XML as text.
writer.WriteCData(xmlTable.OuterXml);
Another option (thanks DJQuimby) is to encode the XML to some XML compatible format (say base64) - note that the encoding used must be XML compatible and that some encoding schemes will increase the size of the encoded document (base64 adds ~30%).

C# XDocument needs to be parsed to XML again

I have a Xdocument that is populated as follows:
XDocument xDoc = XDocument.Parse(new StreamReader(response.GetResponseStream()).ReadToEnd());
This gives me an XDocument that looks as follows:
<GetReportAsXMLString>
<report>
<reportItem Count ="562..................
</GetReportAsXMLStringResult>
Anything between the tag is all just a giant string(Black). How would I get this portion of the document to format as XML? The tag is also part of the string. I just don't know how to make this show it as not XML.
Thanks
It's hard to say from your description, but it looks like you'll need to first parse the response stream (valid xml), which contains another xml document (as a string). You'll need to extract the string from the 'outer' xml document and parse it into a new one:
psuedocode:
XDocument outer = response.GetResponseStream();
String innerXml = outer.Element("report").Value;
XDocument inner = XDocument.Parse(innerXml);
You have this XML content :
<GetReportAsXMLString>
<report>
<reportItem Count =\"562\"/>
<reportItem Count =\"562\"/>
</report>
</GetReportAsXMLString>
and you want to extract only the "reportItem" nodes?
If so you can do this:
string xml = "<GetReportAsXMLString><report><reportItem Count =\"562\"/><reportItem Count =\"562\"/></report></GetReportAsXMLString>";
XDocument xDoc = XDocument.Parse(xml);
IEnumerable<XElement> elList = xDoc.Descendants().Where(x => x.Name.LocalName.Equals("report")).Descendants().Where(x => x.Name.LocalName.Equals("reportItem"));

I need to convert an XML string into an XmlElement

I'm looking for the simplest way to convert a string containing valid XML into an XmlElement object in C#.
How can you turn this into an XmlElement?
<item><name>wrench</name></item>
Use this:
private static XmlElement GetElement(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
return doc.DocumentElement;
}
Beware!!
If you need to add this element to another document first you need to Import it using ImportNode.
Suppose you already had a XmlDocument with children nodes, And you need add more child element from string.
XmlDocument xmlDoc = new XmlDocument();
// Add some child nodes manipulation in earlier
// ..
// Add more child nodes to existing XmlDocument from xml string
string strXml =
#"<item><name>wrench</name></item>
<item><name>screwdriver</name></item>";
XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();
xmlDocFragment.InnerXml = strXml;
xmlDoc.SelectSingleNode("root").AppendChild(xmlDocFragment);
The Result:
<root>
<item><name>this is earlier manipulation</name>
<item><name>wrench</name></item>
<item><name>screwdriver</name>
</root>
Use XmlDocument.LoadXml:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");
XmlElement root = doc.DocumentElement;
(Or in case you're talking about XElement, use XDocument.Parse:)
XDocument doc = XDocument.Parse("<item><name>wrench</name></item>");
XElement root = doc.Root;
You can use XmlDocument.LoadXml() to do this.
Here is a simple examle:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("YOUR XML STRING");
I tried with this snippet, Got the solution.
// Sample string in the XML format
String s = "<Result> No Records found !<Result/>";
// Create the instance of XmlDocument
XmlDocument doc = new XmlDocument();
// Loads the XML from the string
doc.LoadXml(s);
// Returns the XMLElement of the loaded XML String
XmlElement xe = doc.DocumentElement;
// Print the xe
Console.out.println("Result :" + xe);
If any other better/ efficient way to implement the same, please let us know.
Thanks & Cheers

Categories

Resources