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");
Related
Let's say I have the following XML file
<root></root>
and the following file
<child>
<more-childeren> </more-childeren>
</child>
How do I insert the second file into the first file to create the following file:
<root>
<child>
<more-childeren> </more-childeren>
</child>
</root>
I am receiving the second file as a XPathNavigator. What would be the fastest way to insert the XPathNavigator into the XML file?
If you work with XPathNavigators over editable tree structures like XmlDocument/XmlNode then use the AppendChild method taking an XPathNavigator https://learn.microsoft.com/en-us/dotnet/api/system.xml.xpath.xpathnavigator.appendchild?view=net-5.0#System_Xml_XPath_XPathNavigator_AppendChild_System_Xml_XPath_XPathNavigator_. That is at least the most convenient and API supported way, "the fastest" is a different criteria you would need to test.
A simple example working for me with .NET framework is
XmlDocument doc = new XmlDocument();
doc.LoadXml(#"<root></root>");
XPathNavigator nav = doc.DocumentElement.CreateNavigator();
XPathDocument doc2;
using (XmlReader xr = XmlReader.Create(new StringReader(#"<child>
<more-childeren> </more-childeren>
</child>")))
{
doc2 = new XPathDocument(xr);
}
XPathNavigator nav2 = doc2.CreateNavigator();
nav2.MoveToFirstChild();
nav.AppendChild(nav2);
doc.Save(Console.Out);
The call nav2.MoveToFirstChild(); seems to be crucial to not get the exception you mention in the comments.
Try following xml linq :
string xml1 = "<root></root>";
string xml2 = "<child><more-childeren> </more-childeren></child>";
XDocument doc1 = XDocument.Parse(xml1);
XElement root = doc1.Root;
root.Add(XElement.Parse(xml2));
I am having an XML file. I would like to create a new node and append it at the end of the XML file and save it back to memory.
<IntCal>
<User>
<Date>12/09/2012</Date>
<Client>abcd</Client>
<Jewellery>Others</Jewellery>
<ROI>7.5</ROI>
<Description>Some Description</Description>
</User>
<IntCal>
I would like to create a new <User> element. Any idea how to do it.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);
XmlNode root_node;
//XmlNodeList nodeList = xmlDoc.SelectNodes("/IntCal/User");
XmlDocument new_node = new XmlDocument();
root_node = xmlDoc.CreateElement("IntCal");
xmlDoc.AppendChild(root_node);
Thanks
Reference: http://msdn.microsoft.com/en-us/library/fw1ys7w6(v=vs.100).aspx
XmlElement elem = xmlDoc.CreateElement("User");
xmlDoc.DocumentElement.AppendChild(elem);
If you want to go the LINQ route, you could do:
XDocument xDoc = XDocument.Load(xmlFile);
xDoc.Element("IntCal")
.Add(new XElement("User"));
Personally, I'd opt for the XDocument and use LINQ but either way works.
Reference: http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx
XmlDocument is an old school, why don't you use XDocument, simple and easy:
XDocument xDoc = XDocument.Load(xmlFile);
xDoc.Root.Add(new XElement("User",
new XElement("Client", "John"),
new XElement("Jewellery", "Others")));
xDoc.Save(xmlFile);
References:
XDocument or XmlDocument
Performance: XDocument versus XmlDocument
How do I get information from an xml document?
I have an xml document at c:\temp\data.xml and am using visual studio.
The closest I can figure is:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(#"C:\temp\data.xml");
date = xdoc.SelectSingleNode("/forcast_informat…
The XML document looks like this:
<?xml version="1.0"?>
-<xml_api_reply version="1">
-<weather section="0" row="0" mobile_zipped="1" mobile_row="0" tab_id="0" module_id="0">
-<forecast_information>
etc etc...
<current_date_time data="2012-08-09 21:53:00 +0000"/>
etc, etc...
All I want to do is grab that date of 2012-08-09 21:53:00 +0000...any suggestions?
This should do the trick:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(#"C:\temp\data.xml");
XmlNode dataAttribute = xdoc.SelectSingleNode("/xml_api_reply/weather/forecast_information/current_date_time/#data");
Console.WriteLine(dataAttribute.Value);
Try this. This will load current date and time for every forecast:
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(XMLDocumentPath);
XmlNodeList NodeList = XMLDoc.SelectNodes("/xml_api_reply/weather/forecast_information/");
foreach(XmlNode Node in NodeList)
{
string DTime = Node["current_date_time"].InnerText;
//Do something with DTime
}
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
I have XML stored in string variable:
<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>
Here I want to change XML tag <ItemMasterList> to <Masterlist>. How can I do this?
System.Xml.XmlDocument and the associated classes in that same namespace will prove invaluable to you here.
XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("MasterList");
docNew.AppendChild(newRoot);
newRoot.InnerXml = doc.DocumentElement.InnerXml;
String xml = docNew.OuterXml;
I know i am a bit late, but just have to add this answer as no one seems to know about this.
XDocument doc = XDocument.Parse("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
doc.Root.Name = "MasterList";
Which returns the following:
<MasterList>
<ItemMaster>
<fpartno>xxx</fpartno>
<frev>000</frev>
<fac>Default</fac>
</ItemMaster>
</MasterList>
You can use LINQ to XML to parse the XML string, create a new root and add the child elements and attributes of the original root to the new root:
XDocument doc = XDocument.Parse("<ItemMasterList>...</ItemMasterList>");
XDocument result = new XDocument(
new XElement("Masterlist", doc.Root.Attributes(), doc.Root.Nodes()));
Using the XmlDocument way, you can do this as follows (and keep the tree intact):
XmlDocument oldDoc = new XmlDocument();
oldDoc.LoadXml("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
XmlNode node = oldDoc.SelectSingleNode("ItemMasterList");
XmlDocument newDoc = new XmlDocument();
XmlElement ele = newDoc.CreateElement("MasterList");
ele.InnerXml = node.InnerXml;
If you now use ele.OuterXml is will return: (you you just need the string, otherwise use XmlDocument.AppendChild(ele) and you will be able to use the XmlDocument object some more)
<MasterList>
<ItemMaster>
<fpartno>xxx</fpartno>
<frev>000</frev>
<fac>Default</fac>
</ItemMaster>
</MasterList>
As pointed by Will A, we can do it that way but for case where InnerXml equals the OuterXml the following solution will work out:
// Create a new Xml doc object with root node as "NewRootNode" and
// copy the inner content from old doc object using the LastChild.
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("NewRootNode");
docNew.AppendChild(newRoot);
// The below line solves the InnerXml equals the OuterXml Problem
newRoot.InnerXml = oldDoc.LastChild.InnerXml;
string xmlText = docNew.OuterXml;