C# 获取XML元素并将值替换为字符串?
从加载的XML文件中获取XML元素,我认为我需要修改我的逻辑,因为我需要将元素值作为字符串而不是子体获取,然后将值替换为自定义值,然后我需要将XML作为字符串消息,即doc.ToString() 请建议如何从加载的XML中获取字符串形式的元素值,然后在内存中替换该值C# 获取XML元素并将值替换为字符串?,c#,xml,string,C#,Xml,String,从加载的XML文件中获取XML元素,我认为我需要修改我的逻辑,因为我需要将元素值作为字符串而不是子体获取,然后将值替换为自定义值,然后我需要将XML作为字符串消息,即doc.ToString() 请建议如何从加载的XML中获取字符串形式的元素值,然后在内存中替换该值 string filePath = @"C:\FileMessageTemplates\Outright\"; //File path XDocument doc = XDocument.Load(filePath + "First
string filePath = @"C:\FileMessageTemplates\Outright\"; //File path
XDocument doc = XDocument.Load(filePath + "First_Test_1.xml"); //Loads document
string catsId = doc.Descendants("CatsId").SingleOrDefault().ToString(); //Want to get element CatsId here and then replace the value, then save it as string
doc.ToString();
这是XML
<Test xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemS-instance" xmlns:user="urn:my-scripts" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<FeedTest>
<ActionId xsi:nil="true" />
<ReplyToMessageId>12345</ReplyToMessageId>
<Source>PAPER</Source>
<Condition />
<FixingRate xsi:nil="true" />
<CatsId>TAAH20181105X0000579</CatsId>
</FeedTest>
</Test>
12345
纸张
TAAH20181105X0000579
如果没有XML示例,这是最好的猜测
string filePath = @"C:\FileMessageTemplates\Outright\"; //File path
string path = Path.Combine(filePath ,"First_Test_1.xml");
XDocument doc = XDocument.Load(path); //Loads document
var list = doc.Root.Elements("CatsId")
.Select(element => element.Value)
.ToList();
foreach (string value in list)
{
Console.WriteLine(value);
}
// just the first one:
string catidstring = (string)list.FirstOrDefault();
注:如果只有一个,则更简单:
using System;
using System.Xml.Linq;
using System.Xml.XPath;
public class Program
{
public static void Main()
{
string _xml = @"<Test xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchemS-instance' xmlns:user='urn:my-scripts' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
<FeedTest>
<ActionId xsi:nil='true' />
<ReplyToMessageId>12345</ReplyToMessageId>
<Source>PAPER</Source>
<Condition />
<FixingRate xsi:nil='true' />
<CatsId>TAAH20181105X0000579</CatsId>
</FeedTest>
</Test>";
XDocument doc = XDocument.Parse(_xml);
var catIdText = doc.Root.Element("FeedTest").Element("CatsId").Value;
Console.WriteLine(catIdText);
// update or use XPath to update...
doc.Root.Element("FeedTest").Element("CatsId").Value = catIdText+"Happy day";
//doc.XPathSelectElement("//Test/FeedTest/CatsId").Value =catIdText+"Happy day";
var catIdUpdatedText = doc.Root.Element("FeedTest").Element("CatsId").Value;
Console.WriteLine(catIdUpdatedText);
// save it again:
string filePath = @"C:\FileMessageTemplates\Outright\"; //File path
string path = Path.Combine(filePath ,"First_Test_NEW.xml");
xdoc.Save(path);
}
}
使用系统;
使用System.Xml.Linq;
使用System.Xml.XPath;
公共课程
{
公共静态void Main()
{
字符串_xml=@”
12345
纸张
TAAH20181105X0000579
";
XDocument doc=XDocument.Parse(_xml);
var catIdText=doc.Root.Element(“FeedTest”).Element(“CatsId”).Value;
Console.WriteLine(catIdText);
//更新或使用XPath更新。。。
doc.Root.Element(“FeedTest”).Element(“CatsId”).Value=catIdText+“快乐日”;
//doc.XPathSelectElement(“//Test/FeedTest/CatsId”).Value=catIdText+“快乐日”;
var catIdUpdatedText=doc.Root.Element(“FeedTest”).Element(“CatsId”).Value;
Console.WriteLine(catIdUpdatedText);
//再次保存:
字符串filePath=@“C:\FileMessageTemplates\Outlet\”;//文件路径
string path=path.Combine(filePath,“First_Test_NEW.xml”);
xdoc.Save(路径);
}
}
这应该可以。在xml中查找名为“CatsId”的子体。然后替换结果元素上的值。这将更新内存中文档中的元素值
string filePath = @"C:\FileMessageTemplates\Outright\First_Test_1.xml";
XDocument doc = XDocument.Load(filePath);
// Get the proper element (assuming one exists, without namespace)
XElement catsIdElement = doc.Descendants("CatsId").SingleOrDefault();
// Replace the value
catsIdElement.Value = "My new value";
// Tadaa, updated CatsId value
Console.WriteLine(doc.ToString());
如果你贴出一个XML@MarkSchultheiss假设XMLMy guess适用于该XML…我没有测试它…不适用我需要一个简单的解决方案mateI添加了一个简单的示例-我将其解析为字符串,但您的加载应该可以工作。