我有一个字符串,它看起来像这样:在一个XML字符串替换字符串
newNodeXML = "<item id="qDf73w8emTg" parent_id="weLPzE243de" type="suite">
<content>
<name>Three</name>
</content>
</item>"
在我的[的WebMethod],我试图取代PARENT_ID(在运行时随机生成的)像此:
Regex myRegex = new Regex(@""" parent_id=""(.*?)"" type=""");
newNodeXML = myRegex.Replace(newNodeXML, "d43df2qT45");
请注意,例如/演示缘故,我已经在第二行中使用“d43df2qT45”的上方。我实际上也会随机生成它。
我的问题这里是这样的结果。 我不想这样的:
<item id="qDf73w8emTgd43df2qT45suite">
<content>
<name>Three</name>
</content>
</item>
相反,这正是我想要它是:
<item id="qDf73w8emTg" parent_ID="d43df2qT45" type="suite">
<content>
<name>Three</name>
</content>
</item>
附:我已经尝试过一些例子/谷歌搜索,我所能找到的所有例子都让我感觉如此。
正则表达式可能是这样做的错误方式。检查出http://stackoverflow.com/questions/2424613/xml-changing-the-value-of-an-attribute – nullforce
“parent_id”值的长度是否固定并且在前面已知? –
为什么你不反序列化这个内容,改变你所需要的,然后再次序列化它? – Crasher