how can append to xml - c#

i have this xml.
<project>
<user>
<id>1</id>
<name>a</name>
</user>
<user>
<id>2</id>
<name>b</name>
</user>
</project>
now how can append a new element like this between element <project></project>
<user>
<id>3</id>
<name>c</name>
</user>

string xml =
#"<project>
<user>
<id>1</id>
<name>a</name>
</user>
<user>
<id>2</id>
<name>b</name>
</user>
</project>";
XElement x = XElement.Load(new StringReader(xml));
x.Add(new XElement("user", new XElement("id",3),new XElement("name","c") ));
string newXml = x.ToString();

If you mean using C# then probably the simplest way is to load the xml up into an XmlDocument object and then add a node representing the additional element.
e.g. something like:
string filePath = "original.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlElement root = xmlDoc.DocumentElement;
XmlNode nodeToAdd = doc.CreateElement(XmlNodeType.Element, "user", null);
XmlNode idNode = doc.CreateElement(XmlNodeType.Element, "id", null);
idNode.InnerText = "1";
XmlNode nameNode = doc.CreateElement(XmlNodeType.Element, "name", null);
nameNode.InnerText = "a";
nodeToAdd.AppendChild(idNode);
nodeToAdd.AppendChild(nameNode);
root.AppendChild(nodeToAdd);
xmlDoc.Save(filePath); // Overwrite or replace with new file name
But you haven't said where the xml fragments are - in files/strings?

If you have the below XML file:
<CATALOG>
<CD>
<TITLE> ... </TITLE>
<ARTIST> ... </ARTIST>
<YEAR> ... </YEAR>
</CD>
</CATALOG>
and you have to add another <CD> node with all its child nodes:
using System.Xml; //use the xml library in C#
XmlDocument document = new XmlDocument(); //creating XML document
document.Load(#"pathOfXmlFile"); //load the xml file contents into the newly created document
XmlNode root = document.DocumentElement; //points to the root element (catalog)
XmlElement cd = document.CreateElement("CD"); // create a new node (CD)
XmlElement title = document.CreateElement("TITLE");
title.InnerXML = " ... "; //fill-in the title value
cd.AppendChild(title); // append title to cd
XmlElement artist = document.CreateElement("ARTIST");
artist.InnerXML = " ... ";
cd.AppendChild(artist);
XmlElement year = document.CreateElement("YEAR");
year.InnerXML = " ... ";
cd.AppendChild(year);
root.AppendChild(cd); // append cd to the root (catalog)
document.save(#"savePath");//save the document

Related

c#: Add element to second nesting in xml

I have a XML which looks like:
<users>
<user id="0">
<name>John</name>
<lastName>Smith</lastName>
<bills>
<bill id="0">
<name>Water</name>
<forMonth>2013-12-01</forMonth>
<money>235</money>
<lastDayToPay>2014-01-02</lastDayToPay>
<payed>False</payed>
</bill>
<bill id="1">
<name>Telephone</name>
<forMonth>2013-11-01</forMonth>
<money>690</money>
<lastDayToPay>2014-01-01</lastDayToPay>
<payed>True</payed>
</bill>
</bills>
</user>
How can i add new bill for the user, i have problem accessing "bills" node and adding element to it. I'm using c#.
use following code
XmlDocument myDocument = new XmlDocument();
myDocument.Load(XMLFile);
XmlNode newNode = myDocument.CreateElement("bill");
//add values;
var requiredNode = myDocument.ChildNodes.OfType<XmlElement>().Where(o => o.Name == "bills").First();
requiredNode.AppendChild(newNode);
myDocument.Save(XMLFile);

How do I modify xml internal attributes

I am trying to edit a xml file.
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(#"C:\\Users\\Vahid\\Desktop\\HG\\HG\\HG\\singleM.kml");
XmlNode myNode = myXmlDocument.SelectSingleNode(
"/kml/Document/Placemark/Point/coordinates");
myNode.Value = coordinates;
myXmlDocument.Save(#"C:\\Users\\Vahid\\Desktop\\HG\\HG\\HG\\singleM.kml");
and this is my xml (.kml) file:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>change.kml</name>
<Style id="sn_ylw-pushpin"></Style>
<Placemark>
<Point>
<coordinates>0, 0,0</coordinates>
</Point>
<name>12</name>
</Placemark>
</Document>
</kml>
Xml namespaces:
XmlNamespaceManager ns = new XmlNamespaceManager(myXmlDocument.NameTable);
ns.AddNamespace("kml", "http://www.opengis.net/kml/2.2");
XmlNode myNode = myXmlDocument.SelectSingleNode("/kml:kml/kml:Document/kml:Placemark/kml:Point/kml:coordinates", ns);
myNode.InnerText = coordinates;
Note that there is nothing special about "kml" / "kml:" here - it could just as well be:
XmlNamespaceManager ns = new XmlNamespaceManager(myXmlDocument.NameTable);
ns.AddNamespace("x", "http://www.opengis.net/kml/2.2");
XmlNode myNode = myXmlDocument.SelectSingleNode("/x:kml/x:Document/x:Placemark/x:Point/x:coordinates", ns);
myNode.InnerText = coordinates;
The important point is that each of your elements are in the namespace http://www.opengis.net/kml/2.2; the AddNamespace is just adding an alias for that, so that we can talk about the namespace conveniently - we then write the xpath using the alias, and pass the namespace-manager to the SelectSingleNode methods.

xml error:System.Xml.XPath.XPathException: Expression must evaluate to a node-set

I am trying to find a node in my xml file but getting the error ( see title)?
// instantiate XmlDocument and load XML from file
XmlDocument doc = new XmlDocument();
doc.Load(#"C:\temp\test2.xml");
var node = doc.SelectSingleNode("/Offers/Offer/ID=[text()='1']");
var test = node;
xml
<?xml version="1.0" encoding="utf-8"?>
<Offers>
<Offer>
<Model>AAAA</Model>
<ID>1</ID>
<Name>First offer</Name>
</Offer>
<Offer>
<Model>BBBB</Model>
<ID>2</ID>
<Name>Second offer</Name>
</Offer>
</Offers>
Remove the = after ID:
var node = doc.SelectSingleNode("/Offers/Offer/ID=[text()='1']");
becomes:
var node = doc.SelectSingleNode("/Offers/Offer/ID[text()='1']");

Is it possible to insert two child Nodes with same name but different Attributes?

I would like to create an XML file like this:
<?xml version="1.0" encoding="UTF-8"?>
<text>
<languages>
<language id =1>English</language>
<language1 id=2>Slovenian</language1>
</languages>
<strings>
<line id=1>
<string lid=1>camera</string>
<string lid=2>kamera</string1>
</line>
<line id=2>
<string lid=1>lens</string>
<string1 lid=2>leka</string1>
</line>
</strings>
</text>
I am trying with the following code in C# (Windows Forms Application):
XmlDocument xmldoc = new XmlDocument();
XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", null, null);
xmldoc.AppendChild(xmldec);
XmlElement root = xmldoc.CreateElement("text");
xmldoc.AppendChild(root);
XmlElement lang = xmldoc.CreateElement("languages");
XmlElement languages = xmldoc.CreateElement("language");
languages.SetAttribute("id", "1");
languages.InnerText = "English";
lang.PrependChild(languages);
languages.SetAttribute("id", "2");
languages.InnerText = "Slovenian";
lang.PrependChild(languages);
XmlElement lines = xmldoc.CreateElement("strings");
XmlElement line = xmldoc.CreateElement("line");
XmlElement lineinner = xmldoc.CreateElement("string");
line.SetAttribute("id", "1");
lineinner.SetAttribute("lid", "1");
lineinner.InnerText = "some english text";
line.AppendChild(lineinner);
line.SetAttribute("id", "1");
lineinner.SetAttribute("lid", "2");
lineinner.InnerText = "some slovenian text";
line.AppendChild(lineinner);
lines.AppendChild(line);
root.AppendChild(lang);
root.AppendChild(lines);
Metodi.SerializeXMLToXML(xmldoc);
And i end up with the following result:
<?xml version="1.0" encoding="UTF-8"?>
<text>
<languages>
<language id="2">Slovenian</language>
</languages>
<strings>
<line id="2">
<string lid="2">some slovenian text</string>
</line>
</strings>
</text>
After every next AppendChild() replaces the previous is there any way to achieve this??
Re-initialize it every time is one solution:
XmlDocument xmldoc = new XmlDocument();
XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", null, null);
xmldoc.AppendChild(xmldec);
XmlElement root = xmldoc.CreateElement("text");
xmldoc.AppendChild(root);
XmlElement lang = xmldoc.CreateElement("languages");
XmlElement languages = xmldoc.CreateElement("language");
languages.SetAttribute("id", "1");
languages.InnerText = "English";
lang.PrependChild(languages);
languages = xmldoc.CreateElement("language");
languages.SetAttribute("id", "2");
languages.InnerText = "Slovenian";
lang.PrependChild(languages);
XmlElement lines = xmldoc.CreateElement("strings");
XmlElement line = xmldoc.CreateElement("line");
XmlElement lineinner = xmldoc.CreateElement("string");
line.SetAttribute("id", "1");
lineinner.SetAttribute("lid", "1");
lineinner.InnerText = "some english text";
line.AppendChild(lineinner);
line = xmldoc.CreateElement("string");
line.SetAttribute("id", "1");
lineinner.SetAttribute("lid", "2");
lineinner.InnerText = "some slovenian text";
line.AppendChild(lineinner);
lines.AppendChild(line);
root.AppendChild(lang);
root.AppendChild(lines);
Metodi.SerializeXMLToXML(xmldoc);
Nodes can only have one parent, hence your problem. A nicer way to resolve this, though, might be to use XML literals. It has come to my attention that C# does not support XML literals. This doesn't change the fact that you can write this in a nicer way using XML serialization or loops and such.

How to find the name of the root node of a given xml file

Im using c#.net windows form application. I have a xml file whose name is hello.xml and it goes like this
<?xml version="1.0" encoding="utf-8" ?>
<languages>
<language>
<key>abc</key>
<value>hello how ru</value>
</language>
<language>
<key>def</key>
<value>i m fine</value>
</language>
<language>
<key>ghi</key>
<value>how abt u</value>
</language>
</languages>
How can i get the root node i.e <languages> into a text box. At this time I will have the xml file name. i.e "hello.xml". Using this I should get the root node.
Using LINQ to XML you can do this:
XDocument doc = XDocument.Load("input.xml");
string rootLocalName = doc.Root.Name.LocalName;
textBox1.Text = '<' + rootLocalName + '>';
With XmlDocument you can use this:
XmlDocument doc = new XmlDocument();
doc.Load("input.xml");
string rootName = doc.SelectSingleNode("/*").Name;
Or use the XmlDocument DocumentElement property as shown here:
XmlDocument doc = new XmlDocument();
doc.Load("hello.xml");
string root = doc.DocumentElement.Name;
textBox1.Text = "<" + root + ">";

Categories