Asp.net Linq to Xml learning notes

Source: Internet
Author: User

In addition, I have learned to Entity before, so I am more comfortable learning.
Below is a piece of underlying code in the project. Write down a memo. It would be better if you could help a beginner learn about Linq to Xml.
XML file format:
Copy codeThe Code is 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 addition, deletion, modification, and query
Copy codeThe Code is as follows:
Private string docName = string. Empty; // configuration file path
# Region ISystemModuleConfigService Member
/// <Summary>
/// Add
/// </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>
/// Obtain the 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>
/// Obtain 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 the 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 whether the ChildSystem data with Name = name 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;
}

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.