XmlDocument/XmlDataDocument objects contains CreateElement and CreateAttribute methods that are used to add nodes to the XmlDocument object.
XmlElement Contains a property Attributes Property which is an XmlAttributeCollection . this is inhirets from XmlNamedNodeMap class
Demo:
Prepare xml document programmatic:
XmlDocument xmldoc = new XmlDocument();
xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0", "utf-8", "yes"));
XmlElement x=xmldoc.CreateElement("Root");
XmlElement newjob = xmldoc.CreateElement("jobs"); // parent
XmlElement jobid = xmldoc.CreateElement("job_ids");
jobid.InnerText = txtJobid.Text;
XmlElement jobname = xmldoc.CreateElement("job_desc");
jobname.InnerText = txtJobdesc.Text;
XmlElement minsal = xmldoc.CreateElement("min_lvl");
minsal.InnerText = txtminlvl.Text;
XmlElement maxsal = xmldoc.CreateElement("max_lvl");
maxsal.InnerText = txtmaxlvl.Text;
newjob.AppendChild(jobid);
newjob.AppendChild(jobname);
newjob.AppendChild(minsal);
newjob.AppendChild(maxsal);
x.AppendChild(newjob);
xmldoc.AppendChild(x); // End the Root.
xmldoc.Save("C:\\jobs12345.xml"); // the the xml file to disk
Label1.Text = "create xml document programmatically...";
How to add Attributes to xmlElements?
For adding Attributes to XmlElement we have a class is XmlAttribute class.
Ex:
Prepare xml document programmatic along with Attributes:
XmlDocument myDoc = new XmlDocument();
myDoc.AppendChild(myDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"));
//The Father
XmlElement Father = myDoc.CreateElement("Father");
//Create attributes
XmlAttribute FatherName = myDoc.CreateAttribute("Name");
XmlAttribute FatherAge = myDoc.CreateAttribute("Age");
//Add values to attributes
FatherName.Value = "My father";
FatherAge.Value = "65";
//Append attributes to their correspondent element
Father.Attributes.Append(FatherName);
Father.Attributes.Append(FatherAge);
//Add the element to the Familie node
Familie.AppendChild(Father);
//The mother
XmlElement Mother = myDoc.CreateElement("Mother");
//Create attributes
XmlAttribute MotherName = myDoc.CreateAttribute("Name");
XmlAttribute MotherAge = myDoc.CreateAttribute("Age");
//Add values to attributes
MotherName.Value = "My mother";
MotherAge.Value = "60";
//Append attributes to their correspondent element
Mother.Attributes.Append(MotherName);
Mother.Attributes.Append(MotherAge);
//Add the element to the Familie node
Familie.AppendChild(Mother);
//The sister
XmlElement Sister = myDoc.CreateElement("Sister");
//Create attributes
XmlAttribute SisterName = myDoc.CreateAttribute("Name");
XmlAttribute SisterAge = myDoc.CreateAttribute("Age");
//Add values to attributes
SisterName.Value = "My sister";
SisterAge.Value = "20";
//Append attributes to their correspondent element
Sister.Attributes.Append(SisterName);
Sister.Attributes.Append(SisterAge);
//Add the element to the Familie node
Familie.AppendChild(Sister);
//The brother
XmlElement Brother = myDoc.CreateElement("Brother");
//Create attributes
XmlAttribute BrotherName = myDoc.CreateAttribute("Name");
XmlAttribute BrotherAge = myDoc.CreateAttribute("Age");
//Add values to attributes
BrotherName.Value = "My brother";
BrotherAge.Value = "21";
//Append attributes to their correspondent element
Brother.Attributes.Append(BrotherName);
Brother.Attributes.Append(BrotherAge);
//Add the element to the Familie node
Familie.AppendChild(Brother);
//wrapp all elements nodes in wrapper node
myDoc.AppendChild(Familie); myDoc.Save(@"C:\myFamilssie.xml"); Console.WriteLine("Document is generated successfully!!");
Note: Don’t write XmlAttributes for declaration of xml document .to specify declaration we have
XmlDocument myDoc = new XmlDocument();
myDoc.AppendChild(myDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"));
****
No comments:
Post a Comment