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