更改 xml 节点值
本文关键字:节点 xml 更改 | 更新日期: 2023-09-27 18:32:40
我有一个如下所述的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>
我必须将节点值"值"从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格式获得的节点值没有更新的"值"节点的值,并带有注释"此处的问题"
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
直到此索引。
使用 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;
}
问题是您正在尝试从
试试这个
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);
}
}
}