Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何搜索xml节点值,然后在c中为该元素创建新属性#_C#_.net_Xml_Xpath - Fatal编程技术网

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

C# 如何搜索xml节点值,然后在c中为该元素创建新属性#,c#,.net,xml,xpath,C#,.net,Xml,Xpath,我使用c#.net 4.6 xpath搜索id值为的节点,并在找到该节点时为父元素创建一个新属性。我有一个这样的id值列表,需要迭代并创建属性来生成新的xml文档。我尝试了以下操作,但不起作用。XPathNavigator.MoveTo方法似乎将源导航器替换为movedto元素,从而丢失所有其他内容。这不是实现这一目标的正确方法吗?你能给我一些建议吗 请参阅下面的代码片段: publicationDoc.LoadXml(publicationXPathNav.OuterXml);

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

请参阅下面的代码片段:

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>

iso:pub:std:FDIS:74824
假的
标准
92764155
假的
标准
92802320
假的
标准
92801989

使用字典尝试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);
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序167
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
Dictionary dict=文档子体(“id”)
.GroupBy(x=>(字符串)x,y=>y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
字符串id=“92764155”;
字符串filename=“filename”;
if(dict.ContainsKey(id))
{
dict[id].SetAttributeValue(“文件名”,文件名);
}
}
}
}

我通过不使用XPathNavigator和仅依赖XMLDoCunt解决了这个问题。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.

谢谢你的回答。我当然会把这些带上。

请编辑你的帖子,并添加一个想要的输出XML。考虑使用现代API在<代码> Stask.XML.LINQ