Change xml node value

2019-08-24 02:31发布

I have an xml like mentioned below:

<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>

I have to change the node value 'Value' from C:\Users\laitkor\Downloads\BulkTest826.mp4 to BulkTest826.mp4.

I have tried changing the value by using:

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

But the value of nodes I am getting in XML format does not have the updated Value for 'Value' node at line mentioned with comment 'issue here'

标签: c# xml
4条回答
乱世女痞
2楼-- · 2019-08-24 02:35
        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\", "");
            }
        }

I'm supposing that you want to this more than once for different files. I used to replace the C:\Users\laitkor\Downloads\, if the location can be different you can find index of last \ and substring till this index.

查看更多
可以哭但决不认输i
3楼-- · 2019-08-24 02:47

Use XElement from System.Linq.Xml namespace. Much more intuitive XML parsing.

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";
}
查看更多
Animai°情兽
4楼-- · 2019-08-24 02:47

You can do like this:

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

        foreach (XmlNode node in nodes)
        {
            node.SelectSingleNode("Value").InnerText = fileName;
        }

Problem is that you are trying to get from node /Attributes/Attribute node /Attributes/Attribute/Value

查看更多
Root(大扎)
5楼-- · 2019-08-24 02:48

try this

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);
        }
    }
}
查看更多
登录 后发表回答