Asp. NET Operation XML

Source: Internet
Author: User

1.c# Creating an XML document

Class Program
{
static void Main (string[] args)
{
XElement element = new XElement ("person");
XElement element1 = new XElement ("Son1");
XElement element2 = new XElement ("Son2");
Element. Setattributevalue ("type", "human");
Element1. Setattributevalue ("Age", 15);
Element1. SetValue ("Zhang San");
Element. ADD (ELEMENT1);
Element. ADD (Element2);

String strxml= element. ToString ();
Console.WriteLine (strxml);
Console.ReadLine ();
}
}

2.c# reading an XML document

The XML document is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<person>
<men>
<men1 age= ">"
Tom
</men1>
<men2 age= ">"
John doe
</men2>
</men>
<women>
<women1 age= ">"
Xiaoli
</women1>
<women2 age= ">"
Mimi
</women2>
</women>
</person>

The C # code is as follows:

using (Stream stream = File.openread (@ "D:\ complete Project \2014-3-14\kongzhitai\xml\xml\xml.xml"))
{
using (StreamReader reader = new StreamReader (stream))
{
XDocument doc = xdocument.load (reader);
int count = doc. Root.elements (). Count ();
foreach (Var element1 in Doc. Root.elements ())
{
foreach (Var element2 in element1. Nodes ())
{
if (element2. GetType () ==typeof (XElement))
{
XElement ele = (XElement) element2;
Console.WriteLine ("Name:" +ele. Value+ "Age:" +ele. Attribute ("Age"). Value);
}

}
}
}
}


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.