. Net FrameWork SDK example

Source: Internet
Author: User

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>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.