繁体   English   中英

如何搜索xml节点值,然后在c#中为该元素创建新属性

[英]how to search for a xml node value, then create new attribute for that element in c#

我正在使用 c# .net 4.6 xpath 搜索具有 id 值的节点,并在找到时为父元素创建一个新属性。 我有一个此类 id 值的列表,我需要对其进行迭代并创建属性以生成新的 xml 文档。 我尝试了以下但不起作用。 XPathNavigator.MoveTo 方法似乎用移动到的元素替换了源导航器,从而丢失了所有其他内容。 这不是实现这一目标的正确方法吗? 你能指点一下吗?

请参阅下面的代码片段:

publicationDoc.LoadXml(publicationXPathNav.OuterXml);
            XPathNavigator publicationNav = publicationDoc.CreateNavigator();

    foreach (IListBlobItem item in contentDirectory.ListBlobs())
                {
                    var blob = (CloudBlob)item;
                    string contentId = blob.Name;
                    
                    XPathNavigator contentRefNav = publicationNav.SelectSingleNode($@"//releaseItem/contentRef[id = {"'" + contentId + "'"}]/..");
    
                    if (contentRefNav != null)
                    {
                        publicationNav.MoveTo(contentRefNav); // here publicationNav gets replaced by contentRefNav
                        publicationNav.CreateAttribute("", "fileName", "", contentFileName);
                    }
                }

// once finished with the foreach I was hoping to be able to save the publicationNav.OuterXml to a new file with the newly added attributes.

这是一个小的缩减示例源 xml 数据:

<publicationsRoot>
    <publication>
        <urn>iso:pub:std:FDIS:74824</urn>
        <releaseItems>
            <releaseItem>
                <languageNeutral>false</languageNeutral>
                <type>STANDARD</type>
                <contentRef>
                    <id>92764155</id>
                </contentRef>
            </releaseItem>
            <releaseItem>
                <languageNeutral>false</languageNeutral>
                <type>STANDARD</type>
                <contentRef>
                    <id>92802320</id>
                </contentRef>
            </releaseItem>
            <releaseItem>
                <languageNeutral>false</languageNeutral>
                <type>STANDARD</type>
                <contentRef>
                    <id>92801989</id>
                </contentRef>
            </releaseItem>
        <releaseItems>  
    </publication>
</publicationsRoot>

用字典试试 xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication167
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, XElement> dict = doc.Descendants("id")
                .GroupBy(x => (string)x, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            string id = "92764155";
            string filename = "filename";

            if (dict.ContainsKey(id))
            {
                dict[id].SetAttributeValue("filename", filename);
            }

        }
    }
}

我通过不使用 XPathNavigator 而仅依赖 XMLDocuent 设法解决了这个问题。 看起来 XPathNavigator 更适合相对路径,而我的要求是搜索特定节点并就地更新 xml 文档。

publicationDoc.LoadXml(publicationXPathNav.OuterXml);

    foreach (IListBlobItem item in contentDirectory.ListBlobs())
                {
                    var blob = (CloudBlob)item;
                    string contentId = blob.Name;

                    XmlNode contentRefNode = publicationDoc.SelectSingleNode($@"//releaseItem/contentRef[id = {"'" + contentId + "'"}]/..");
    
                    if (contentRefNode != null)
                    {
                        XmlAttribute fileName = publicationDoc.CreateAttribute("fileName");
                    fileName.Value = contentFileName + contentFileExt;
                    contentRefNode.Attributes.SetNamedItem(fileName);
                    }
                }

// once finished with the foreach I was hoping to be able to save the publicationNav.OuterXml to a new file with the newly added attributes.

感谢所有的答案。 我当然会接受这些。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM