[英]Find and replace text inside xml document using regular expression
I am using c# console app to get xml document. 我正在使用c#控制台应用程序来获取xml文档。 Now once xmldocument is loaded i want to search for specific href tag:
现在,一旦加载了xmldocument,我想搜索特定的href标签:
href="/abc/def
inside the xml document. 在xml文档中。
once that node is found i want to strip tag completly and just show Hello. 找到该节点后,我想完全剥离标签并仅显示Hello。
<a href="/abc/def">Hello</a>
I think i can simply get the tag using regex. 我想我可以使用正则表达式简单地获得标签。 But can anyone please tell me how can i remove the href tag completly using regex?
但是有人可以告诉我如何使用正则表达式完全删除href标签吗?
xml & html same difference: tagged content. xml和html的不同点是:标记的内容。 xml is stricter in it's formatting.
xml的格式更加严格。 for this use case I would use transformations and xpath queries rebuild the document.
对于这种用例,我将使用转换和xpath查询来重建文档。 As @Yahia stated, regex on tagged documents is typically a bad idea.
正如@Yahia所说,在带标签的文档上使用正则表达式通常不是一个好主意。 the regex for parsing is far to complex to be affective as a generic solution.
作为通用解决方案,解析的正则表达式非常复杂,难以发挥作用。
The most popular technology for similar tasks is called XPath. 用于完成类似任务的最流行技术称为XPath。 (It is also a key component of XQuery and XSLT.) Would the following perhaps solve your task, too?
(它也是XQuery和XSLT的关键组件。)以下内容是否也可以解决您的任务?
root.SelectSingleNode("//a[@href='/abc/def']").InnerText = "Hello";
You could try 你可以试试
string x = @"<?xml version='1.0'?>
<EXAMPLE>
<a href='/abc/def'>Hello</a>
</EXAMPLE>";
System.Xml.XmlDocument doc = new XmlDocument();
doc.LoadXml(x);
XmlNode n = doc.SelectSingleNode("//a[@href='/abc/def']");
XmlNode p = n.ParentNode;
p.RemoveChild(n);
System.Xml.XmlNode newNode = doc.CreateNode("element", "a", "");
newNode.InnerXml = "Hello";
p.AppendChild(newNode);
Not really sure if this is what you are trying to do but it should be enough to get you headed in right direction. 不太确定这是否是您要尝试的方法,但是它足以使您朝正确的方向前进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.