【问题标题】:Looking for help finding an element in an XML XDocument and updating attributes寻求帮助在 XML XDocument 中查找元素并更新属性
【发布时间】:2015-12-01 18:00:47
【问题描述】:

我正在学习 XML 查询和 Xdocument,但在更新现有元素的属性时遇到了问题。这是我的 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-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 文件结构示例:

<?xml version="1.0" encoding="utf-8"?>
<Officers>
  <Officer ID="Dust" Latitude="4" Longitude="5" Status="3" />
</Officers>

【问题讨论】:

    标签: c# .net xml linq


    【解决方案1】:

    您正在检查一个名为 OfficerID 的属性,但您只使用新的 OfficerID 变量创建了一个名为 ID 的属性。

    改变任何一个

    where (string)el.Attribute("OfficerID") == OfficerID
    

    成为

    where (string)el.Attribute("ID") == OfficerID
    

    改变

    new XAttribute("ID", OfficerID),
    

    成为

    new XAttribute("OfficerID", OfficerID),
    

    另一件可能很关键的事情是,即使你找到了警官,直到你完成搜索才开始搜索。可枚举延迟执行,直到这样做。因此,对于您的 foreach,将其更改为:

    foreach (XElement el in officer.ToList())
    

    ToList() 执行可枚举项,ToArray() 等其他人也是如此。它也是一个安全网,以防您删除元素。

    附注,与问题分开:

    由于您在 foreach 和 new officer 分支中都调用了doc.Save(),因此将 save 放在方法的底部作为最后发生的事情。

    【讨论】:

      【解决方案2】:

      除了@Chuck Savage给出的解决方案,如果每个警官都有唯一的ID,你可以考虑做一些改变: 1 可以退单官。这样你就可以避免运行 foreach 代码块。 2 检查官员是否存在更新它,否则创建。无需设置更新真假。

      【讨论】:

      • 也谢谢。我能够按照上面的方法让它工作,但我同意你的建议会让这个更干净,并且也会这样做
      猜你喜欢
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2020-03-29
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多