C# 更改xml节点值

C# 更改xml节点值,c#,xml,C#,Xml,我有一个如下所述的xml: <Attributes> <Attribute> <EntryID>0</EntryID> <ContractID>227860</ContractID> <FieldID>10882</FieldID> <GroupID>0</GroupID> <InstanceID>0</Instan

我有一个如下所述的xml:

<Attributes>
  <Attribute>
    <EntryID>0</EntryID>
    <ContractID>227860</ContractID>
    <FieldID>10882</FieldID>
    <GroupID>0</GroupID>
    <InstanceID>0</InstanceID>
    <Value>C:\Users\laitkor\Downloads\BulkTest826.mp4</Value>
    <CreatedBy>615</CreatedBy>
    <CreatedOn>12/1/2014 6:51:04 AM</CreatedOn>
    <UpdatedBy>615</UpdatedBy>
    <UpdatedOn>12/1/2014 6:51:04 AM</UpdatedOn>
  </Attribute>
</Attributes>

但是我得到的XML格式的节点值没有注释“issue here”中提到的行中“value”节点的更新值。

使用System.Linq.XML命名空间中的XElement。更直观的XML解析

var xml=
XElement.Parse(yourXml); // or load XElement.Load(file);
var node = xml.Descendants()
    .FirstOrDefault(x => x.Name == "Value" && x.Parent.Name =="Attribute"); 

node.Value="BulkTest826.mp4"; // If you only need one
// Or
var valueNodes = xml.Descendants("Value");
foreach(var val in valueNodes)
{
  // val.Value = "You new value";
}
您可以这样做:

        XmlNodeList nodes = doc.SelectNodes("Attributes/Attribute");

        foreach (XmlNode node in nodes)
        {
            node.SelectSingleNode("Value").InnerText = fileName;
        }
问题是您试图从节点/属性/属性节点/属性/属性/属性/值获取

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(nodes);
        bool isMultimedia = false;
        XmlNodeList xnList = xml.SelectNodes("/Attributes/Attribute");

        foreach(XmlNode node in xnList)
        {
            XmlNode n1 = node.SelectSingleNode("Value");

            //I will suppose that you need to do that for more than one Value node
            if(n1.InnerText.Contains(@"C:\Users\laitkor\Downloads\"))
            {
                n1.InnerText = n1.InnerText.Replace(@"C:\Users\laitkor\Downloads\", "");
            }
        }
我假设您希望对不同的文件多次执行此操作。我曾经替换过
C:\Users\laitkor\Downloads\
,如果位置可能不同,您可以找到上一个
\
子字符串的索引,直到找到这个索引。

试试这个

using System;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;                 
using System.IO;

public class Program
{
    public static void Main()
    {
        //XElement xml = XElement.Load(xmlFile); //Load from file
        XElement xml=XElement.Parse(@"<Attributes>  <Attribute>    <EntryID>0</EntryID>    <ContractID>227860</ContractID>    <FieldID>10882</FieldID>    <GroupID>0</GroupID>    <InstanceID>0</InstanceID>    <Value>C:\Users\laitkor\Downloads\BulkTest826.mp4</Value>    <CreatedBy>615</CreatedBy>    <CreatedOn>12/1/2014 6:51:04 AM</CreatedOn>    <UpdatedBy>615</UpdatedBy>    <UpdatedOn>12/1/2014 6:51:04 AM</UpdatedOn>  </Attribute></Attributes>");
        var valueElements = xml.XPathSelectElements("//Attribute/Value");

        foreach(XElement valueElement in valueElements)
        {           
            valueElement.Value=Path.GetFileName(valueElement.Value);
            Console.WriteLine(valueElement.Value);
        }
    }
}
使用系统;
使用System.Linq;
使用System.Xml.Linq;
使用System.Xml.XPath;
使用System.IO;
公共课程
{
公共静态void Main()
{
//XElement xml=XElement.Load(xmlFile);//从文件加载
XElement xml=XElement.Parse(@“0 227860 10882 0 0 C:\Users\laitkor\Downloads\BulkTest826.mp4 615 12/1/2014 6:51:04 AM 615 12/1/2014 6:51:04 AM”);
var valueElements=xml.XPathSelectElements(“//属性/值”);
foreach(valueElements中的XElement valueElement)
{           
valueElement.Value=Path.GetFileName(valueElement.Value);
Console.WriteLine(valueElement.Value);
}
}
}

检查我的解决方案并告诉我这是否是您所需要的。抱歉..但这也不起作用。我在最后第二行(“//issue here”)得到的XML(节点)是相同的,并且值没有更新。@ajitasrivastava使用这一行您正在更改值,如果issue here行不起作用,您可能应该查看不起作用的方法。您可以调试它,并看到此代码更改了xml节点值。选中AddBulkContractField方法。@ajitasrivastava是否保存了xml文档?与此doc.Save()@ajitasrivastava一样,您也将作为参数放入SiteProvider.ContractBulk.AddBulkContractField节点。在代码中,它只是一个字符串。试着这样做:SiteProvider.ContractBulk.AddBulkContractField(doc.InnerText,contracd,groupID,sequenId,1);
using System;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;                 
using System.IO;

public class Program
{
    public static void Main()
    {
        //XElement xml = XElement.Load(xmlFile); //Load from file
        XElement xml=XElement.Parse(@"<Attributes>  <Attribute>    <EntryID>0</EntryID>    <ContractID>227860</ContractID>    <FieldID>10882</FieldID>    <GroupID>0</GroupID>    <InstanceID>0</InstanceID>    <Value>C:\Users\laitkor\Downloads\BulkTest826.mp4</Value>    <CreatedBy>615</CreatedBy>    <CreatedOn>12/1/2014 6:51:04 AM</CreatedOn>    <UpdatedBy>615</UpdatedBy>    <UpdatedOn>12/1/2014 6:51:04 AM</UpdatedOn>  </Attribute></Attributes>");
        var valueElements = xml.XPathSelectElements("//Attribute/Value");

        foreach(XElement valueElement in valueElements)
        {           
            valueElement.Value=Path.GetFileName(valueElement.Value);
            Console.WriteLine(valueElement.Value);
        }
    }
}