reading XML files in asp.net

Source: Internet
Author: User
Tags add foreach config key net string version root directory
Asp.net|xml

Read:
Open a file (assuming Web.config is in the root directory)
String Filename=server.mappath ("/") + @ "Webapplication1\web.config";
XmlDocument xmldoc= New XmlDocument ();
XmlDoc. Load (filename);

Get Top level Node list
XmlNodeList Topm=xmldoc. Documentelement.childnodes;
foreach (xmlelement element in topm)
{
if (element. Name.tolower () = = "appsettings")
{

To get the child nodes of the node
XmlNodeList nodelist=element. ChildNodes;

if (nodelist. Count >0)
{
DropDownList1.Items.Clear ();

foreach (XmlElement el in nodelist)//Read 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;


Also here you can modify the value of the element and save it later.
El. attributes["Value"]. Value=this. TextBox2.Text;
}


}

}

}

XmlDoc. Save (filename);

Add an element under 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:
<appSettings>
<add key= "Password" value= "admin"/>
<add>ltp</add>
</appSettings>

Add an element under a node and add two properties:
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= "a";

Elem. Setattributenode (XA);
Elem. Setattributenode (XA2);


XmlDoc. Save (filename);

}

Effect:
<appSettings>
<add key= "Password" value= "admin"/>
<add key= "LTP" value= "the" "/>
</appSettings>

To 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)
{

To get the child nodes of the node
XmlNodeList nodelist=element. ChildNodes;

foreach (XmlElement el in nodelist)//Read element value
{
if (el. attributes["Key"]. Value==itemname)
{
Element. RemoveChild (EL);
}

}//Loop Element

}//Get the Group

}//Cycle Group

Doc. Save (xmlfile); Be sure to keep it, otherwise it won't 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)
{

}
}/**//* reads XML data in two XML ways
<aaa>
<bb>something</bb>
<cc>something</cc>
</aaa>

<aaa>
<add key= "123" value= "321"/>
</aaa>

The first method of/**//* * *
Ds. READXML ("Your xmlfile name");
Container.DataItem ("BB");
Container.DataItem ("CC");
Ds. ReadXmlSchema ("Your xmlfile name");

/**//* the second method of * *
<aaa>
<add key= "123" value= "321"/>
</aaa>
How do I write if I have to find 123 and then 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


/**//* the third method: selectSingleNode reads two formats of XML *---/
--------------------------------------------------------------------
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<appSettings>
<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/appsettings/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>
<appsettings>
<add key= ConnectionString "value=" Data source= Yf User id=ctm_dbo;password=123 "/>
</appsettings>
</configuration>
* *--------------------- -----------------------------------------------* *
XmlNode Node=doc. selectSingleNode ("/configuration/appsettings/add");
if (node!=null)
{
String k=node. attributes["Key"]. Value;
String V=node. attributes["Value"]. Value;
Node=null;
}
*--------------------------------------------------------------------*
XmlNode Node=doc. selectSingleNode ("/configuration/appsettings/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 end of the file.
Nr. MoveToAttribute ("value");
String S=nr. Value;
Node=null;
}



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.