How to read node values in xml files in C #

Source: Internet
Author: User
I'm glad to give you an answer!
Xml is actually a simple local database
I just made a simple one .. But the truth is the same.
// Xml file information
<Abc>
<Field> 100 </Field>
<Item>
<Id> 1 </id>
<Name> zhangsan </name>
<Sex> male </sex>
</Item>
<Item>
<Id> 2 </id>
<Name> lisi </name>
<Sex> male </sex>
</Item>
</Abc>

// Entity class.
Public class Information
{
Private string id;
Public string Id
{
Get {return id ;}
Set {id = value ;}
}
Private string name;
Public string Name
{
Get {return name ;}
Set {name = value ;}
}
Private string sex;
Public string Sex
{
Get {return sex ;}
Set {sex = value ;}
}
Public Information ()
{

}
Public Information (string id, string name, string sex)
{
This. Id = id;
This. Name = name;
This. Sex = sex;
}
}
// Read the file information in xml
List <Information> list = new List <Information> ();
// Instantiate xml
XmlDocument xml = new XmlDocument ();
// Read the xml file
Xml. Load (@ "E: \ C # \ S2C # \ DLCL \ Print computer \ MyComputer \ XulieHua \ XML. xml"); // your xml address
String id = "";
String name = "";
String sex = "";
Information info = null;
//// // ******* Start to read xml file information cyclically ********/
///////////////
Foreach (XmlNode node in xml. ChildNodes)
{
If (node. Name = "abc ")
{
Foreach (XmlNode node1 in node. ChildNodes)
{
If (node1.Name = "item ")
{
Foreach (XmlNode node2 in node1.ChildNodes)
{
Switch (node2.Name)
{
Case "id ":
Id = node2.InnerText;
Break;
Case "name ":
Name = node2.InnerText;
Break;
Default:
Sex = node2.InnerText;
Break;
}
}
Info = new Information (id, name, sex );
// Save the information to the set
List. Add (info );
}
}
}
}
All the information in xml is in the list set .. Simple .. Hey ..
Of course, you can do multiple tables and multiple field attributes ..

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.