XmlDocument. CreateAttribute effect demonstration
Using System;
Using System. IO;
Using System. Xml;
Namespace CreateAttribute
{
/// <Summary>
/// Summary of Class1.
/// </Summary>
Class Class1
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main (string [] args)
{
//
// TODO: Add code here to start the application
//
XmlDocument doc = new XmlDocument ();
Doc. LoadXml ("<book genre = novel ISBN = 1-861001-57-5>" +
"<Title> Pride And Prejudice </title>" +
"</Book> ");
// Create an attribute.
XmlAttribute attr = doc. CreateAttribute ("publisher ");
Attr. Value = "WorldWide Publishing ";
// Add the new node to the document.
Doc. DocumentElement. SetAttributeNode (attr );
Console. WriteLine ("Display the modified XML ...");
Doc. Save (Console. Out );
}
}
}
The effect is as follows:
Display the modified XML...
<? Xml version = "1.0" encoding = "gb2312"?>
<Book genre = "novel" ISBN = "1-861001-57-5" publisher = "WorldWide Publishing">
<Title> Pride And Prejudice </title>
</Book> Press any key to continue
XmlDocument. CreateNode method effect demonstration
Using System;
Using System. Xml;
Namespace CreateNode
{
/// <Summary>
/// Summary of Class1.
/// </Summary>
Class Class1
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main (string [] args)
{
//
// TODO: Add code here to start the application
//
XmlDocument doc = new XmlDocument ();
Doc. LoadXml ("<book>" +
"<Title> Oberons Legacy </title>" +
& Quot; <price> 5.95 & quot;/price & quot; +
"</Book> ");
// Create a new element node.
XmlNode newElem;
NewElem = doc. CreateNode (XmlNodeType. Element, "pages ","");
NewElem. InnerText = "290 ";
Console. WriteLine ("Add the new element to the document ...");
XmlElement root = doc. DocumentElement;
Root. AppendChild (newElem );
Console. WriteLine ("Display the modified XML document ...");
Console. WriteLine (doc. OuterXml );
}
}
}
Effect:
Add the new element to the document...
Display the modified XML document...
<Book> <title> Oberons Legacy </title> <price> 5.95 </price> <pages> 290 </pages> </book>