我有给定结构的XML:C# - 从字符串模板中插入XML元素
<ArrayOfElement>
<Element>
<ID>1</ID>
<Value>Value1</Value>
</Element>
<Element>
<ID>2</ID>
<Value>Value2</Value>
</Element>
<Element>
<ID>3</ID>
<Value>Value3</Value>
</Element>
...
</ArrayOfElement>
我想插入另一元素与该模板(但只有具有给定ID号元素存在):
<Element>
<ID>$ID$</ID>
<Value>$VALUE$</Value>
</Element>
所以我的想法是加载模板string
,利用一切给予更换$ID$
和$VALUE$
。然后我解析这个字符串为XmlDocumentFragment
。然后我需要看看具有此ID的元素是否已经存在,并且仅当没有其他元素存在时我想插入新元素。
但我不知道如何找到一个存在。
我目前的做法是:
// Load the XML with the ArrayOfElement
var allElements = new XmlDocument();
allElements.Load("file.xml");
// Load the template and insert id and value
var xmlTemplate = File.ReadAllText("template.xml");
var fragment = allElements.CreateDocumentFragment();
fragment.InnerXml = xmlTemplate.Replace("$ID$", _id).Replace("$VALUE$", _value);
// This does not work since it will throw an exception if none is found:
if (allElements.SelectNodes("ArrayOfElement/Element/ID/text() = '" + _id + "'").Count == 0)
{
allElements.appendChild(fragment);
}
而且我真的想用一个模板文件,因为我其实有更复杂的场景中添加这些XML标签在C#中没有一个模板字符串是办法不多的工作。
您可以优化您的方法。先不创建片段元素并搜索原始XML,您可以先搜索原始XML,然后搜索创建和插入。 – Gururaj
我知道,但这并不能帮助我解决问题 – danielspaniol