Click here to Skip to main content
11,584,208 members (63,992 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows .NET
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 22-Aug-11 20:14pm
Edited 22-Aug-11 20:27pm
v2
Comments
walterhevedeich at 23-Aug-11 2:15am
   
And what's the problem with the code?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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);
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Do you want to create XML file (document).
use XMLWriter[^] or XML Document [^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I guess you want to know how to use XmlDocument.
Have a look at XML for beginners and experts[^]
  Permalink  
v2
Comments
richcb at 11-Jun-13 10:57am
   
Really? Two years later and you return to change one word?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
nivasmaran at 23-Aug-11 2:40am
   
add.xml is created at run time or we have to create xml file using add item
Söderlund at 24-Aug-11 6:23am
   
add.xml is created during runtime.
It will also be overwritten everytime you run this code.
Member 9741872 at 14-Feb-13 3:57am
   
its not showing ny error but m nt able to find where the newly created xml file stored???
Söderlund at 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 at 17-Jun-14 14:18pm
   
how to save xml file in current project folder..how to download that file ......can tell me
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

using System.Linq;
using System.Xml.Linq;
public void WriteXml() { 
	XElement xml =new XElement("root");
	xml.Save("C:\file.xml");
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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();
}
}
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 430
1 Sergey Alexandrovich Kryukov 369
2 Dave Kreskowiak 260
3 Richard Deeming 220
4 virusstorm 194
0 OriginalGriff 1,663
1 Sergey Alexandrovich Kryukov 1,229
2 Abhinav S 701
3 Dave Kreskowiak 667
4 Suvendu Shekhar Giri 568


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 16 Apr 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100