在xml文件中查找并替换文本 [英] Find and Replace a text in xml file
本文介绍了在xml文件中查找并替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在查找和替换xml文件中的文本时遇到麻烦.我的问题是我必须在xml文件中找到一个文本并将其更改为其他一些文本.但是ex的某些元素标签中的文本:
谢谢
< tmc>
< csc>
< location> vizag
< name> sudeer
< education> mca
Hi,
I am having trouble finding and replacing text in a xml file. My issue is I have to find a text in a xml file and change it to some other text. But the text in some element tag for ex:
Thanks
<tmc>
<csc>
<location>vizag
<name>sudeer
<education>mca
<csc>
<location>vizag</location>
<name>suneel</name>
<education>be</education>
</csc>
<csc>
<location>pune</location>
<name>ashok</name>
<education>mba</education>
</csc>
<csc >
<location>hyderabad</location>
<name>bhaskar</name>
<education>mtech</education>
</csc>
<csc>
<location>santhosh</location>
<name>usa</name>
<education>mca</education>
</csc>
</tmc>
结果应为
The result should be
<tmc>
<csc>
<location>vizag</location>
<name>sudeer</name>
<education>mca</education>
</csc>
<csc>
<location>vizag</location>
<name>suneel</name>
<education>be</education>
</csc>
<csc>
<location>pune</location>
<name>ashok</name>
<education>mba</education>
</csc>
<csc >
<location>hyderabad</location>
<name>bhaskar</name>
<education>mtech</education>
</csc>
<csc>
<location>santhosh</location>
<name>pune</name>
<education>mca</education>
</csc>
</tmc>
推荐答案
我假设您需要执行此操作的通用代码,并且XMLReader和XMLWriter类可以正常工作:
XMLReader [ WMLWriter [ http://msdn.microsoft.com/en-us/library/system. xml.linq.xelement.aspx [ ^ ]
[/Edit]
I assume that you want a general code for doing this, and for that the XMLReader and XMLWriter class would work:
XMLReader[^]
WMLWriter[^]
OR a simpler method:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx[^]
[/Edit]
加载XML文档;阅读XML文档;比较位置;只要找到匹配项,就进行更改,否则将新节点添加为根节点".希望这会有所帮助.
或
您可以使用linq.
Load XML Document; Read XML Document ; compare Location ; whenever you find match do changed else add as new node to Root Node. Hope this will help.
or
you can use linq.
XDocument xmlcat = XDocument.Load(path_for_ur_doc);
XElement root = xmlcat.Element("tmc");
int totalchild = xmlcat.Root.Elements().Count();
XElement firstnode = (XElement)catroot.FirstNode;
for (int i = 0; i < totalchild; i++)
{
if(firstnode.FirstNode.Value == "you_entered_Location")
{
// Do what ever you want to do
break;
}
firstnode = (XElement)imagenode.NextNode;
}
这篇关于在xml文件中查找并替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文