【问题标题】:Change xml node value更改 xml 节点值
【发布时间】:2014-12-01 10:15:46
【问题描述】:

我有一个如下所述的 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>

我必须将节点值“Value”从 C:\Users\laitkor\Downloads\BulkTest826.mp4 更改为 BulkTest826.mp4

我尝试使用以下方法更改值:

  XmlDocument xml = new XmlDocument();
        xml.LoadXml(nodes); 
        bool isMultimedia = false;
        XmlNodeList xnList = xml.SelectNodes("/Attributes/Attribute");
        foreach (XmlNode xn in xnList)
            {
            int FieldId = Convert.ToInt32(xn["FieldID"].InnerText);
            isMultimedia = true;
            if (isMultimedia) {
            string MultiMediaFilePath = xn["Value"].InnerText;
            createMultimediaFile(FieldId, MultiMediaFilePath, contractID);//todo
            string fileName = MultiMediaFilePath.Substring(MultiMediaFilePath.LastIndexOf('\\', MultiMediaFilePath.Length - 1));
            fileName = fileName.TrimStart('\\');
            xn.SelectSingleNode("/Attributes/Attribute/Value").InnerText = fileName;

                }
            retval = SiteProvider.ContractBulk.AddBulkContractField(nodes, contractID, groupID, sequenId, 1);//issue here
            return retval;
            }

但是我以 XML 格式获取的节点的值没有更新的“值”节点的值,在注释“此处发布”提到的行中

【问题讨论】:

  • 检查我的解决方案并告诉我这是否是您需要的。

标签: c# xml


【解决方案1】:
        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\,如果位置可以不同,你可以找到最后一个\substring的索引直到这个索引。

【讨论】:

  • 对不起..但这也不起作用。我在倒数第二行('//issue here')得到的 XML(节点)是相同的,值没有更新。
  • @ajitasrivastava 使用此行您正在更改值,如果此处的问题行不起作用,您可能应该查看不起作用的方法。您可以对其进行调试,并看到此代码更改了 xml 节点值。检查 AddBulkContractField 方法。
【解决方案2】:

试试这个

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);
        }
    }
}

【讨论】:

    【解决方案3】:

    使用 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";
    }
    

    【讨论】:

      【解决方案4】:

      你可以这样做:

              XmlNodeList nodes = doc.SelectNodes("Attributes/Attribute");
      
              foreach (XmlNode node in nodes)
              {
                  node.SelectSingleNode("Value").InnerText = fileName;
              }
      

      问题是您试图从节点 /Attributes/Attribute 节点 /Attributes/Attribute/Value 获取

      【讨论】:

      • @ajitasrivastava 你保存了 xml 文件吗?喜欢这个 doc.Save()。
      • @ajitasrivastava 您也被放入 SiteProvider.ContractBulk.AddBulkContractField 节点作为参数。在您的代码中,它只是一个字符串。像这样尝试:SiteProvider.ContractBulk.AddBulkContractField(doc.InnerText, contractID, groupID, sequenId, 1);
      猜你喜欢
      • 2012-08-07
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      相关资源
      最近更新 更多