Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
4.07/5 (6 votes)
See more:
hello friends how to create xml document Programmatically in c#

i tried
xmldocument doc=new xmldocument();
doc.save(@"c:/program files/file name/add.xml");


give me some suggestion
Posted
Updated 22-Aug-11 20:27pm
v2
Comments
walterhevedeich 23-Aug-11 2:15am    
And what's the problem with the code?

using System;
using System.Xml;

public class GenerateXml {
private static void Main() {
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);

XmlNode productsNode = doc.CreateElement("products");
doc.AppendChild(productsNode);

XmlNode productNode = doc.CreateElement("product");
XmlAttribute productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "01";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);

XmlNode nameNode = doc.CreateElement("Name");
nameNode.AppendChild(doc.CreateTextNode("Java"));
productNode.AppendChild(nameNode);
XmlNode priceNode = doc.CreateElement("Price");
priceNode.AppendChild(doc.CreateTextNode("Free"));
productNode.AppendChild(priceNode);

// Create and add another product node.
productNode = doc.CreateElement("product");
productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "02";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);
nameNode = doc.CreateElement("Name");
nameNode.AppendChild(doc.CreateTextNode("C#"));
productNode.AppendChild(nameNode);
priceNode = doc.CreateElement("Price");
priceNode.AppendChild(doc.CreateTextNode("Free"));
productNode.AppendChild(priceNode);

doc.Save(Console.Out);
}
}
 
Share this answer
 
Do you want to create XML file (document).
use XMLWriter[^] or XML Document [^]
 
Share this answer
 
I guess you want to know how to use XmlDocument.
Have a look at XML for beginners and experts[^]
 
Share this answer
 
v2
Comments
Richard C Bishop 11-Jun-13 10:57am    
Really? Two years later and you return to change one word?
C#
public void CreateXML()

        {

            XDocument doc = new XDocument(new XDeclaration("1.0", "utf-16", "yes"),

               new XElement("RootNode", new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"), new XAttribute("Name", "X_SoftwareUpdate"), new XAttribute("LocaleId", "1033"),

                   new XElement("ChildNode",

                       new XElement("UpdateXMLDescriptionItem", new XAttribute("PropertyName", "_Product"), new XAttribute("UIPropertyName", ""),

                           new XElement("MatchRules",

                               new XElement("string", "''Product:X''")

                               ))),

                       new XElement("ChildNode",

                       new XElement("UpdateXMLDescriptionItem", new XAttribute("PropertyName", "IsSuperseded"), new XAttribute("UIPropertyName", ""),

                           new XElement("MatchRules",

                               new XElement("string", "false")

                               ))),

                       new XElement("ChildNode",

                       new XElement("UpdateXMLDescriptionItem", new XAttribute("PropertyName", "_UpdateClassification"), new XAttribute("UIPropertyName", ""),

                           new XElement("MatchRules",

                               new XElement("string", "''UpdateClassification:X''")

                               )))));

            doc.Save(@"C:\test.xml");

        }



source : http://www.liberalcode.com/2014/04/your-one-stop-on-how-to-effectively.html[^]
 
Share this answer
 
C#
XmlDocument XD = new XmlDocument();
XmlNode Root = XD.AppendChild(XD.CreateElement("Root"));
XmlNode Child = Root.AppendChild(XD.CreateElement("Child"));
XmlAttribute ChildAtt =Child.Attributes.Append(XD.CreateAttribute("Attribute"));
ChildAtt.InnerText = "My innertext";
Child.InnerText = "Node Innertext";
XD.Save("Add.xml");


I think this is the easiest approach.
I havent tested this code so it might contain minor errors.
 
Share this answer
 
Comments
Nivas Maran 23-Aug-11 2:40am    
add.xml is created at run time or we have to create xml file using add item
Per Söderlund 24-Aug-11 6:23am    
add.xml is created during runtime.
It will also be overwritten everytime you run this code.
ajitmlv 14-Feb-13 3:57am    
its not showing ny error but m nt able to find where the newly created xml file stored???
Per Söderlund 15-Feb-13 15:46pm    
XD.Save("C:/Dir/File.xml");
Saves the file in C:/Dir directory.
When only file name is given it should be saved in the same directory as the running exe that is creating the file.
If I remember it right.
srikanth492 17-Jun-14 14:18pm    
how to save xml file in current project folder..how to download that file ......can tell me
C#
using System.Linq;
using System.Xml.Linq;

public void WriteXml() { 
	XElement xml =new XElement("root");
	xml.Save("C:\file.xml");
}
 
Share this answer
 
Comments
Member 11859517 1-Mar-18 1:30am    
how to add child element
Hi
Here is a sample code that you can use to create XML Document:



Code Snippet
XmlDocument xmlDoc;

XmlNode xmlDeclaration;

XmlElement xmlRoot;

XmlElement xmlElem;

xmlDoc = new XmlDocument();



// Declaration

xmlDeclaration = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");

xmlDoc.AppendChild(xmlDeclaration);



// Root element

xmlRoot = xmlDoc.CreateElement("Root");

xmlDoc.AppendChild(xmlRoot);



// Child of the root

xmlElem = xmlDoc.CreateElement("Element");

xmlRoot.AppendChild(xmlElem);



MessageBox.Show(xmlDoc.OuterXml);





And here you can find some samples:

http://msdn2.microsoft.com/pl-pl/library/k44daxya(en-us).aspx

http://www.example-code.com/csharp/xml.asp

http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx

Yours Farhad.
 
Share this answer
 
using System.Xml;


namespace CreateXmlDoc
{
class CreateXml
{
public static void Main(string[] args)
{
CreateXml obj = new CreateXml();
obj.fn_ShowDetails();
}

public void fn_ShowDetails() {

XmlDocument objxml = new XmlDocument();
XmlNode objxmlDecl;
objxmlDecl = objxml.CreateNode(XmlNodeType.XmlDeclaration, "", "");
objxml.AppendChild(objxmlDecl);
XmlElement objRoot;
objRoot = objxml.CreateElement("ROOT");
XmlElement objEle1,objEle2;
objEle1 = objxml.CreateElement("Node1");
objEle1.InnerText ="1";
objRoot.AppendChild(objEle1);
objEle2 = objxml.CreateElement("Node2");
objEle2.InnerText = "2";
objRoot.AppendChild(objEle2);
objxml.AppendChild(objRoot);
Console.WriteLine(objxml.InnerXml);
Console.ReadLine();
}
}
}
 
Share this answer
 
using System;
using System.Xml;
using System.Text;

protected void btnRead_Click(object sender, EventArgs e)
{
ReadXmlFile(Server.MapPath("EmployeeDetails.xml"));
}
protected void btnWrite_Click(object sender, EventArgs e)
{
XmlTextWriter xWriter = new XmlTextWriter(Server.MapPath("EmployeeDetails.xml"), Encoding.UTF8);
xWriter.WriteStartDocument();
//Create Parent element
xWriter.WriteStartElement("EmployeeDetails");
//Create Child elements
xWriter.WriteStartElement("Details");
xWriter.WriteElementString("Name", txtName.Text);
xWriter.WriteElementString("Department", txtDept.Text);
xWriter.WriteElementString("Location", txtLocation.Text);
xWriter.WriteEndElement();

//End writing top element and XML document
xWriter.WriteEndElement();
xWriter.WriteEndDocument();
xWriter.Close();
}

protected void ReadXmlFile(string fileName)
{
string parentElementName = "";
string childElementName = "";
string childElementValue = "";
bool element = false;
lblMsg.Text = "";

XmlTextReader xReader = new XmlTextReader(fileName);
while (xReader.Read())
{
if (xReader.NodeType == XmlNodeType.Element)
{
if (element)
{
parentElementName = parentElementName + childElementName + "
";
}
element = true;
childElementName = xReader.Name;
}
else if (xReader.NodeType == XmlNodeType.Text | xReader.NodeType == XmlNodeType.CDATA)
{
element = false;
childElementValue = xReader.Value;
lblMsg.Text = lblMsg.Text + "" + parentElementName + "
" + childElementName + "

" + childElementValue;
parentElementName = "";
childElementName = "";
}
}
xReader.Close();
}

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900