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