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;
}