Read and Write XML files in C #

Source: Internet
Author: User

Read:
// Open a file (assuming that web. config is in the root directory)
String filename = server. mappath ("/") + @ "webapplication1/Web. config ";
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (filename );

// Obtain the list of top-level nodes
Xmlnodelist topm = xmldoc. documentelement. childnodes;
Foreach (xmlelement element in topm)
{
If (element. Name. tolower () = "appsettings ")
{

// Obtain the subnode of the node
Xmlnodelist nodelist = element. childnodes;

If (nodelist. Count> 0)
{
// Dropdownlist1.items. Clear ();

Foreach (xmlelement El in nodelist) // read the element value
{
// Dropdownlist1.items. Add (El. attributes ["key"]. innerxml );
// This. textbox2.text = El. attributes ["key"]. innertext;
This. textbox2.text = El. attributes ["key"]. value;
This. label1.text = El. attributes ["value"]. value;

// You can modify the element value here and save it later.
// El. attributes ["value"]. value = This. textbox2.text;
}

}

}

}

Xmldoc. Save (filename );

Add an element to a node and set the value:

If (element. Name. tolower () = "appsettings ")
{

Xmlelement ELEM = xmldoc. createelement ("add ");

Element. appendchild (ELEM );
ELEM. innertext = "LTP ";

Xmldoc. Save (filename );

}

Effect:
<Deleetask>
<Add key = "password" value = "admin"/>
<Add> LTP </Add>
</Appsettings>

Add an element under a node and add two attributes:
If (element. Name. tolower () = "appsettings ")
{

Xmlelement ELEM = xmldoc. createelement ("add ");
Element. appendchild (ELEM );

Xmlattribute XA = xmldoc. createattribute ("key ");
Xa. value = "LTP ";

Xmlattribute xa2 = xmldoc. createattribute ("value ");
Xa2.value = "first ";

ELEM. setattributenode (xa );
ELEM. setattributenode (xa2 );

Xmldoc. Save (filename );

}

Effect:
<Deleetask>
<Add key = "password" value = "admin"/>
<Add key = "LTP" value = "first"/>
</Appsettings>

// Add an empty element:
Xmlnode node = Doc. createelement (groupname );
Node. innertext = "";
Doc. lastchild. appendchild (node );

Doc. Save (xmlfile );

Delete a node Element
String itemname = This. listbox1.selecteditem. tostring ();

This. listbox1.items. Remove (this. listbox1.selecteditem );

// Begin del xmlfile
Xmldocument Doc = new xmldocument ();
Doc. Load (xmlfile );

Xmlnodelist topm = Doc. documentelement. childnodes;
Foreach (xmlelement element in topm)
{
If (element. Name = This. combobox1.text)
{

// Obtain the subnode of the node
Xmlnodelist nodelist = element. childnodes;

Foreach (xmlelement El in nodelist) // read the element value
{
If (El. attributes ["key"]. value = itemname)
{
Element. removechild (EL );
}

} // Cyclic Element

} // Get the group

} // Cycle Group

Doc. Save (xmlfile); // be sure to save it; otherwise, it will not work.

// Filter data
Private void reader_xml (string pathflie)
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (pathflie );
Xmlnodelist record1 = xmldoc. documentelement. selectnodes (code [@ ID = '1'])
Int f = 0;
Foreach (xmlnode xnode in record1)
{

}
}/** // * Two XML methods for reading XML data */
<AAA>
<BB> something </BB>
<CC> something </CC>
</AAA>

<AAA>
<Add key = "123" value = "321"/>
</AAA>

/** // * Method 1 */
DS. readxml ("Your xmlfile name ");
Container. dataitem ("BB ");
Container. dataitem ("cc ");
DS. readxmlschema ("Your xmlfile name ");

/** // * Method 2 */
<AAA>
<Add key = "123" value = "321"/>
</AAA>
What should I do if I want to find 123 and get 321?

Using system. xml;
Xmldatadocument xmldoc = new system. xml. xmldatadocument ();
Xmldoc. Load (@ "C:/config. xml ");
Xmlelement ELEM = xmldoc. getelementbyid ("add ");
String STR = ELEM. attributes ["value"]. Value

/** // * Method 3: selectsinglenode reads XML in two formats *---/
--------------------------------------------------------------------
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Connectionstring> Data Source = YF; user id = ctm_dbo; Password = 123 </connectionstring>
</Appsettings>
</Configuration>
--------------------------------------------------------------------------
Xmldocument Doc = new xmldocument ();
Doc. Load (strxmlname );

Xmlnode node = Doc. selectsinglenode ("/configuration/etettings/connectionstring ");
If (node! = NULL)
{
String k1 = node. value; // null
String k2 = node. innertext; // data source = YF; user id = ctm_dbo; Password = 123
String K3 = node. innerxml; // data source = YF; user id = ctm_dbo; Password = 123
Node = NULL;
}

**************************************** ****************************
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "connectionstring" value = "Data Source = YF; user id = ctm_dbo; Password = 123"/>
</Appsettings>
</Configuration>
**--------------------------------------------------------------------**
Xmlnode node = Doc. selectsinglenode ("/configuration/etettings/Add ");
If (node! = NULL)
{
String K = node. attributes ["key"]. value;
String v = node. attributes ["value"]. value;
Node = NULL;
}
*--------------------------------------------------------------------*
Xmlnode node = Doc. selectsinglenode ("/configuration/etettings/Add ");
If (node! = NULL)
{
Xmlnodereader Nr = new xmlnodereader (node );
Nr. movetocontent ();
// Check whether the current node is a content node. If this node is not a content node, the reader jumps forward to the next content node or the end of the file.
Nr. movetoattribute ("value ");
String S = Nr. value;
Node = NULL;
}

 

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.