我有一个像下面提到的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>
我必须更改节点值&#39; Value&#39;从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格式获得的节点的价值没有更新的值&#39; Value&#39;在评论中提到的节点&#39;在这里发布&#39;
答案 0 :(得分: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
的索引,直到此索引。
答案 1 :(得分: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);
}
}
}
答案 2 :(得分:0)
使用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";
}
答案 3 :(得分:0)
你可以这样做:
XmlNodeList nodes = doc.SelectNodes("Attributes/Attribute");
foreach (XmlNode node in nodes)
{
node.SelectSingleNode("Value").InnerText = fileName;
}
问题是你试图从节点/属性/属性节点/属性/属性/值
获取