C# 查找XML XDocument中的元素并更新属性的帮助信息
我正在学习XML查询和Xdocuments,在更新现有元素的属性时遇到了困难。这是我的WCF服务。第二部分工作(创建带有属性的新元素。问题是我的查询不能返回任何结果,代码总是添加新元素)C# 查找XML XDocument中的元素并更新属性的帮助信息,c#,.net,xml,linq,C#,.net,Xml,Linq,我正在学习XML查询和Xdocuments,在更新现有元素的属性时遇到了困难。这是我的WCF服务。第二部分工作(创建带有属性的新元素。问题是我的查询不能返回任何结果,代码总是添加新元素) //this will insert the officer location and status into the xml data file //I read about how to do this at http://prathapk.net/creating-wcf-serv
//this will insert the officer location and status into the xml data file
//I read about how to do this at http://prathapk.net/creating-wcf-service-to-store-read-data-in-xml-database/
//and https://msdn.microsoft.com/en-us/library/bb387041.aspx
public void InsertOfficerData(string OfficerID, double latitude, double longitude, int StatusCode)
{
//open xml file
XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("Officers.xml"));
//linq query to find the element with the officer ID if it exists
IEnumerable<XElement> officer =
from el in doc.Element("Officers").Elements("Officer")
where (string)el.Attribute("OfficerID") == OfficerID
select el;
bool updated = false;
//update officer attributes
foreach (XElement el in officer)
{
//update attributes
el.Attribute("Latitude").Value = Convert.ToString(latitude);
updated = true;
doc.Save(HttpContext.Current.Server.MapPath("Officers.xml"));
}
//if an officer with the id was not found
if (!updated)
{
//add the element with attributes
doc.Element("Officers").Add(new XElement("Officer",
new XAttribute("ID", OfficerID),
new XAttribute("Latitude", latitude),
new XAttribute("Longitude", longitude),
new XAttribute("Status", StatusCode)));
doc.Save(HttpContext.Current.Server.MapPath("Officers.xml"));
}
}
//这将在xml数据文件中插入官员的位置和状态
//我在网站上读到了如何做到这一点http://prathapk.net/creating-wcf-service-to-store-read-data-in-xml-database/
//及https://msdn.microsoft.com/en-us/library/bb387041.aspx
public void InsertOfficerData(证书字符串、双纬度、双经度、int状态码)
{
//打开xml文件
XDocument doc=XDocument.Load(HttpContext.Current.Server.MapPath(“Officers.xml”);
//linq查询以查找具有官员ID的元素(如果存在)
数名军官=
来自文件要素中的el(“高级职员”)。要素(“高级职员”)
其中(字符串)el.Attribute(“OfficeId”)==OfficeId
选择el;
bool updated=false;
//更新军官属性
高级行政人员(高级行政人员)
{
//更新属性
el.Attribute(“纬度”).Value=Convert.ToString(纬度);
更新=真;
doc.Save(HttpContext.Current.Server.MapPath(“Officers.xml”);
}
//如果找不到有身份证的警官
如果(!已更新)
{
//添加具有属性的元素
文件要素(“官员”)。添加(新要素(“官员”),
新的X属性(“ID”,OfficeId),
新的X属性(“纬度”,纬度),
新的X属性(“经度”,经度),
新的XAttribute(“状态”,状态代码));
doc.Save(HttpContext.Current.Server.MapPath(“Officers.xml”);
}
}
我的XML文件结构示例:
您正在检查一个名为
OfficerID
的属性,但您仅使用新的OfficerID
变量创建一个名为ID
的属性
换一个
where (string)el.Attribute("OfficerID") == OfficerID
将来
或
改变
new XAttribute("ID", OfficerID),
将来
另一件可能很关键的事情是,即使您找到了警官,搜索也要等到您成功后才能进行。枚举会延迟执行,直到您成功。因此,对于您的foreach,请将其更改为:
foreach (XElement el in officer.ToList())
ToList()
执行可枚举项,其他如ToArray()
等执行可枚举项。它也是一个安全网,以防移除元素
与问题分开的旁注:
由于在前部和新的军官分支中调用<代码> doc.SAVE()/代码>,将保存在方法的底部作为最后的事情发生。 < P>除了@ Chuck Savage给出的解决方案之外,如果每个军官都有唯一的ID,你可以考虑做出一些改变: 1您可以返回单个人员。这样可以避免运行每个代码块。
2检查该官员是否存在,以其他方式进行更新。无需将更新设置为true和false。谢谢。我能够按照上述方式使其工作,但我同意您的建议将使其更干净,也可以这样做
new XAttribute("OfficerID", OfficerID),
foreach (XElement el in officer.ToList())