1, XPath operation XML, the bottom part of the code is commented, but the full function, the removal of comments is normal use (there are write naming and other conflicts, so commented)
Overall: Full read XML, XML additions and deletions, the specific operation of XML
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using System.Xml;
Using System.Xml.Linq;
namespace XPath read xml.controllers
{
public class Xpathreadxmlcontroller:controller
{
//
GET:/xpathreadxml/
Public ActionResult Index ()
{
#region to complete the original ecological reading of XML
Combing general XML usage, properties, constructors, and using XPath queries
String path = Server.MapPath ("Xml/3502100001.config");
XDocument Xdoc = xdocument.load (path);
String info = Xdoc. Declaration.tostring ();//For specific access to the statement internal information, you can view Xdoc. Declaration.version,xdoc. Declaration.encoding
String[] Declarationinfo=new String[]{xdoc. Declaration.version,xdoc. Declaration.encoding};
#region learning methods, using the
Do not understand the definition, still do not understand is the specific use of Baidu and MSDN
#endregion
Just take the corresponding element in turn, then the attribute what, and finally the value, everything can be taken down
XElement RootNode = Xdoc. Root;
ienumerable<xelement> elelist = rootnode.elements ("ServerIP");
XElement Xele = rootnode.element ("ServerIP");
XAttribute Rootnodeattribute = rootnode.element ("DBAdmin"). Attribute ("type");//Find the properties of the Dbadmin node in the XML here is the XML
String rootnodeattributevalue = Rootnode.element ("DBAdmin"). Attribute ("type"). value;//Specific values
ienumerable<xattribute> rootnodeattributelist = rootnode.element ("DBAdmin"). Attributes ("type");//This is a result set
#endregion
#region Complete traversal of XML (one or two layers), stored to dictionary
String path = Server.MapPath ("Xml/3502100002.config");
XDocument Xdoc = xdocument.load (path);
dictionary<string, string> dicxml = new dictionary<string, string> ();
XElement RootNode = Xdoc. Root;
XNode XNode = rootnode.firstnode;//If this exists internally, it is possible to cast, get the internal value System.Xml.Linq.XElement
XElement Xele = (XElement) xnode;
Dicxml.add (Xele. Name.localname,xele. Value);
ienumerable<xnode> xnodelist = Rootnode.nodes ();
foreach (XNode x in Xnodelist)
//{
XElement Xele = x as xelement;//the label name of the corresponding element can be obtained after conversion
if (Xele! = null)
{//filter comments and empty
Dicxml.add (Xele. Name.localname, Xele. Value);
// }
//}
#endregion
return View ();
}
Public ActionResult Delete () {
# Delete and change
String path = Server.MapPath ("Xml/350210000t.config");
XDocument Xdoc = xdocument.load (path);
dictionary<string, string> dicxml = new dictionary<string, string> ();
XElement RootNode = Xdoc. Root;
XNode XNode = rootnode.firstnode;//If this exists internally, it is possible to cast, get the internal value System.Xml.Linq.XElement
Rootnode.setelementvalue ("Node1", "Test new node");
Rootnode.save (path);
Select the summary point and then add
Xdoc. Element ("GWT"). Setelementvalue ("Node1", "Join new Node");//This new | modify | Delete all can
Xdoc. Element ("GWT"). Setelementvalue ("Node1", null);//Delete, Ps: Other additions and deletions, but also other methods, Remove,add,repalce
Xdoc. Element ("GWT"). Setelementvalue ("NOde1", "Modify");//Modify
Xdoc. Save (path);//This sentence is very important, this is the last modification of the local file
#region Partial modifications
XElement Xele = Xdoc. Element ("GWT"). Element ("Node1");
Xele. Setelementvalue ("Node11", "Test new node");
Xele. Save (path);//Note this save will only save some of the modified items in the file, so use the original global XML
#endregion
#endregion
return View ();
}
Public ActionResult Xpathindex () {
#region Use of XPath
String path = Server.MapPath ("Xml/350210000t.config");
XmlDocument xmldoc = new XmlDocument ();//XM Lyuanshengtai
Xmldoc.load (path);
XmlNodeList xmlList = xmldoc.selectnodes ("/GWT");
XmlNode node = xmllist[0];
XmlNodeList XmlNodeList = Xmldoc.selectnodes ("./GWT");//All <GWT> elements in the current context.
XmlNodeList xmlnodeattrbutelist = Xmldoc.selectnodes ("//serverip[@type = ' databaseinfo ' and @Description = ' linkurl ']") ///Select Multi-attribute, cannot use && this type, but use And,or directly
#endregion
return View ();
}
}
}
2. XML used in the example
<?xml version= "1.0" encoding= "Utf-8"?>
<GWT>
<declarecode>3502100001</declarecode>
<ownercode>3502100001</ownercode>
<TradeCode>3502100001</TradeCode>
<!--111-->
<dbadmin type= "UserInfo" description= "UserName" >sa</DBAdmin>
<DBPwd>gwt123456</DBPwd>
<clientnum/>
<Admin>admin</Admin>
<Password>gwt123456</Password>
<finishdate>2015/12/30 0:00:00</finishdate>
<dbtype/>
<serverip type= "Databaseinfo" description= "Linkurl" >
<link>192.168.1.111,1433</link>
<Info> This is the address of the Access database </Info>
</Serverip>
<serverport/>
<status>0</status>
<trusttype/>
<EntVersion>EB1</EntVersion>
<ispay/>
<UserType>EB1</UserType>
<IsMD5>1</IsMD5>
</GWT>
XPath operation XML