zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

asp.netLinqtoXml学习笔记

2023-06-13 09:14:16 时间
加上之前学习过LinqtoEntity,因此学习起来也比较随心应手。
以下是项目中某个底层的代码,记下做个备忘,如果能给新手学习LinqtoXml带来帮助,那就再好不过了
XML文件的格式:
复制代码代码如下:

<?xmlversion="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增,删,改,查
复制代码代码如下:

privatestringdocName=string.Empty;//配置文件路径
#regionISystemModuleConfigService成员
///<summary>
///添加
///</summary>
///<paramname="name"></param>
///<paramname="controllerName"></param>
///<returns></returns>
publicboolAdd(stringname,stringcontrollerName)
{
XDocumentxDoc=Load(docName);
if(IsExist(name))
{
xDoc.Element("configuration").Element("OPsystemConfig").Element("ChildSystems").Add(newXElement("ChildSystem",
newXElement("Name",name),
newXElement("ControllerName",controllerName)));
xDoc.Save(docName);
returntrue;
}
returnfalse;
}
///<summary>
///修改
///</summary>
///<paramname="name"></param>
///<paramname="controllerName"></param>
///<returns></returns>
publicboolModify(stringname,stringcontrollerName)
{
XDocumentxDoc=Load(docName);
if(!IsExist(name))
{
varquery=fromOpsysteminxDoc.Descendants("ChildSystem")
whereOpsystem.Element("Name").Value==name
selectOpsystem;
foreach(XElementiteminquery)
{
item.Element("ControllerName").Value=controllerName;
}
xDoc.Save(docName);
returntrue;
}
returnfalse;
}
///<summary>
///删除
///</summary>
///<paramname="name"></param>
///<returns></returns>
publicboolRemove(stringname)
{
XDocumentxDoc=Load(docName);
if(!IsExist(name))
{
varquery=fromOpsysteminxDoc.Descendants("ChildSystem")
whereOpsystem.Element("Name").Value==name
selectOpsystem;
query.Remove();
xDoc.Save(docName);
returntrue;
}
returnfalse;
}
///<summary>
///获得列表
///</summary>
///<returns></returns>
publicIList<SystemModuleConfig>GetList()
{
XDocumentxDoc=Load(docName);
List<SystemModuleConfig>list=newList<SystemModuleConfig>();
varquery=fromOpsysteminxDoc.Descendants("ChildSystem")
selectnew
{
Key=Opsystem.Element("Name").Value,
Value=Opsystem.Element("ControllerName").Value
};
foreach(variteminquery)
{
SystemModuleConfigconfig=newSystemModuleConfig();
config.Name=item.Key;
config.ControllerName=item.Value;
list.Add(config);
}
returnlist;
}
///<summary>
///获得一条ChildSystem数据
///</summary>
///<paramname="name"></param>
///<returns></returns>
publicSystemModuleConfigGetModel(stringname)
{
XDocumentxDoc=Load(docName);
SystemModuleConfigmodel=newSystemModuleConfig();
varquery=fromOpsysteminxDoc.Descendants("ChildSystem")
whereOpsystem.Element("Name").Value==name
selectnew
{
Name=Opsystem.Element("Name").Value,
ControllerName=Opsystem.Element("ControllerName").Value
};
foreach(variteminquery)
{
model.Name=item.Name;
model.ControllerName=item.ControllerName;
}
returnmodel;
}
///<summary>
///加载Config文件
///</summary>
///<paramname="path"></param>
///<returns></returns>
publicXDocumentLoad(stringpath)
{
docName=path;
FileInfofile=newFileInfo(docName);
file.IsReadOnly=false;
returnXDocument.Load(docName);
}
///<summary>
///验证Name=name的ChildSystem数据是否存在
///</summary>
///<paramname="name"></param>
///<returns></returns>
privateboolIsExist(stringname)
{
XDocumentxDoc=Load(docName);
varquery=fromOpsysteminxDoc.Descendants("ChildSystem")
whereOpsystem.Element("Name").Value==name
selectnew
{
Name=Opsystem.Element("Name").Value
};
if(query.Count()==0)
{
returntrue;
}
returnfalse;
}