使用 Linq to XML 在正确位置添加新条目(基于数字属性)

问题描述 投票:0回答:1

我想出了这个函数来保存或更新 XML 文件中的条目。

public void SetOutlineInfo(string talksdatabase, int talknumber, string languageCode, bool talkExcluded, DateTime dateExcudedFrom, string excludedNote)
{
    try
    {
        XDocument doc = XDocument.Load(talksdatabase);
        var ptLang = doc.Descendants(languageCode).FirstOrDefault();
        if (ptLang != null)
        {
            var ptInfo = ptLang
                            .Descendants("PublicTalk")
                            .Where(p => p.Attribute("Number").Value == talknumber.ToString()).FirstOrDefault();
            if (ptInfo != null)
            {
                ptInfo.Attribute("Excluded").Value = talkExcluded ? "true" : "false";
                ptInfo.Attribute("ExcludedFromDate").Value = dateExcudedFrom.ToString("yyyy-MM-dd");
                ptInfo.Attribute("ExcludedNote").Value = excludedNote;
            }
            else
            {
                ptLang.Add(new XElement("PublicTalk",
                    new XAttribute("Number", talknumber),
                    new XAttribute("Excluded", talkExcluded),
                    new XAttribute("ExcludedFromDate", dateExcudedFrom.ToString("yyyy-MM-dd")),
                    new XAttribute("ExcludedNote", excludedNote)
                    ));
            }

            doc.Save(talksdatabase);
        }
    }
    catch (Exception ex)
    {
        SimpleLog.Log(ex);
    }
}

唯一的问题是添加新条目不会将其添加到正确的数字位置(

Number
属性)。

我做了一些搜索,它们都显示了记录中的阅读并用

orderby
排序。但我正在写记录。


按照评论中的要求。

这是一个 XML:

<?xml version="1.0" encoding="utf-8"?>
<PublicTalkTitles>
  <eng>
    <PublicTalk Number="47" Excluded="true" ExcludedFromDate="2019-04-01" ExcludedNote="S-147 19.03" />
  </eng>
</PublicTalkTitles>

添加新条目时,请说对话编号 1:

<?xml version="1.0" encoding="utf-8"?>
<PublicTalkTitles>
  <eng>
    <PublicTalk Number="1" Excluded="true" ExcludedFromDate="2023-09-12" ExcludedNote="1" />
    <PublicTalk Number="47" Excluded="true" ExcludedFromDate="2019-04-01" ExcludedNote="S-147 19.03" />
  </eng>
</PublicTalkTitles>
c# linq-to-xml
1个回答
0
投票

根据使用建议

AddBeforeSelf
我修改了功能:

public void SetOutlineInfo(string talksdatabase, int talknumber, string languageCode, bool talkExcluded, DateTime dateExcudedFrom, string excludedNote)
{
    try
    {
        XDocument doc = XDocument.Load(talksdatabase);
        var ptLang = doc.Descendants(languageCode).FirstOrDefault();
        if (ptLang != null)
        {
            var ptInfo = ptLang
                            .Descendants("PublicTalk")
                            .Where(p => p.Attribute("Number").Value == talknumber.ToString()).FirstOrDefault();
            if (ptInfo != null)
            {
                ptInfo.Attribute("Excluded").Value = talkExcluded ? "true" : "false";
                ptInfo.Attribute("ExcludedFromDate").Value = dateExcudedFrom.ToString("yyyy-MM-dd");
                ptInfo.Attribute("ExcludedNote").Value = excludedNote;
            }
            else
            {
                var ptList = ptLang.Descendants("PublicTalk");
                bool bAdd = true;
                foreach ( var pt in ptList )
                {
                    if (Convert.ToInt32(pt.Attribute("Number").Value) > talknumber)
                    {
                        pt.AddBeforeSelf(new XElement("PublicTalk",
                            new XAttribute("Number", talknumber.ToString()),
                            new XAttribute("Excluded", talkExcluded),
                            new XAttribute("ExcludedFromDate", dateExcudedFrom.ToString("yyyy-MM-dd")),
                            new XAttribute("ExcludedNote", excludedNote)
                            ));
                        bAdd = false;
                        break;
                    }
                }

                if(bAdd)
                {
                    ptLang.Add(new XElement("PublicTalk",
                        new XAttribute("Number", talknumber.ToString()),
                        new XAttribute("Excluded", talkExcluded),
                        new XAttribute("ExcludedFromDate", dateExcudedFrom.ToString("yyyy-MM-dd")),
                        new XAttribute("ExcludedNote", excludedNote)
                        ));
                }
            }

            doc.Save(talksdatabase);
        }
    }
    catch (Exception ex)
    {
        SimpleLog.Log(ex);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.