asp.net LINQ to XML Learning Notes _ practical Tips

Source: Internet
Author: User
Coupled with the previous study of LINQ to Entity, so learning is also more spontaneous should also launch.
Here are some of the underlying code in the project, write a memo, and it would be great if you could help beginners learn LINQ to XML.
Format of XML file:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<OPsystemConfig>
<MemberCenter>
<DomainName>DomainName</DomainName>
<ProtocolName>ProtocolName</ProtocolName>
<APIKey>APIKey</APIKey>
<AESKey>AESKey</AESKey>
<AESVI>AESVI</AESVI>
</MemberCenter>
<ChildSystems>
<ChildSystem>
<Name>Content</Name>
<ControllerName>ContentManager</ControllerName>
</ChildSystem>
<ChildSystem>
<Name>Image</Name>
<ControllerName>ImageManager</ControllerName>
</ChildSystem>
<ChildSystem>
<Name>Comment</Name>
<ControllerName>CommentManager</ControllerName>
</ChildSystem>
<ChildSystem>
<Name>Vote</Name>
<ControllerName>VoteManager</ControllerName>
</ChildSystem>
</ChildSystems>
</OPsystemConfig>
</configuration>

XML Add, delete, change, check
Copy Code code as follows:

private string docname = String. empty;//configuration file path
#region Isystemmoduleconfigservice Members
<summary>
Add to
</summary>
<param name= "Name" ></param>
<param name= "Controllername" ></param>
<returns></returns>
public bool Add (string name, String controllername)
{
XDocument Xdoc = Load (DocName);
if (isexist (name))
{
Xdoc.element ("Configuration"). Element ("Opsystemconfig"). Element ("Childsystems"). ADD (New XElement ("Childsystem",
New XElement ("name", name),
New XElement ("Controllername", controllername));
Xdoc.save (DocName);
return true;
}
return false;
}
<summary>
Modify
</summary>
<param name= "Name" ></param>
<param name= "Controllername" ></param>
<returns></returns>
public bool Modify (string name, String controllername)
{
XDocument Xdoc = Load (DocName);
if (! Isexist (name))
{
var query = from Opsystem in xdoc.descendants ("Childsystem")
where Opsystem.element ("Name"). Value = = Name
Select Opsystem;
foreach (XElement item in query)
{
Item. Element ("Controllername"). Value = Controllername;
}
Xdoc.save (DocName);
return true;
}
return false;
}
<summary>
Delete
</summary>
<param name= "Name" ></param>
<returns></returns>
public bool Remove (string name)
{
XDocument Xdoc = Load (DocName);
if (! Isexist (name))
{
var query = from Opsystem in xdoc.descendants ("Childsystem")
where Opsystem.element ("Name"). Value = = Name
Select Opsystem;
Query. Remove ();
Xdoc.save (DocName);
return true;
}
return false;
}
<summary>
Get list
</summary>
<returns></returns>
Public ilist<systemmoduleconfig> getlist ()
{
XDocument Xdoc = Load (DocName);
list<systemmoduleconfig> list = new list<systemmoduleconfig> ();
var query = from Opsystem in xdoc.descendants ("Childsystem")
Select New
{
Key = Opsystem.element ("Name"). Value,
Value = Opsystem.element ("Controllername"). Value
};
foreach (var item in query)
{
Systemmoduleconfig config = new Systemmoduleconfig ();
Config. Name = Item. Key;
Config. Controllername = Item. Value;
List. Add (config);
}
return list;
}
<summary>
Get a Childsystem data
</summary>
<param name= "Name" ></param>
<returns></returns>
Public Systemmoduleconfig Getmodel (string name)
{
XDocument Xdoc = Load (DocName);
Systemmoduleconfig model = new Systemmoduleconfig ();
var query = from Opsystem in xdoc.descendants ("Childsystem")
where Opsystem.element ("Name"). Value = = Name
Select New
{
Name = Opsystem.element ("name"). Value,
Controllername = Opsystem.element ("Controllername"). Value
};
foreach (var item in query)
{
Model. Name = Item. Name;
Model. Controllername = Item. Controllername;
}
return model;
}
<summary>
Load config file
</summary>
<param name= "Path" ></param>
<returns></returns>
Public XDocument Load (string path)
{
DocName = path;
FileInfo file = new FileInfo (docname);
File. IsReadOnly = false;
Return Xdocument.load (DocName);
}
<summary>
Verify that the Name=name Childsystem data exists
</summary>
<param name= "Name" ></param>
<returns></returns>
private bool Isexist (string name)
{
XDocument Xdoc = Load (DocName);
var query = from Opsystem in xdoc.descendants ("Childsystem")
where Opsystem.element ("Name"). Value = = Name
Select New
{
Name = Opsystem.element ("name"). Value
};
if (query). Count () = = 0)
{
return true;
}
return false;
}

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.