用新值替换整个 XmlElement
我有下面的代码,我在其中获取 html,然后读取所有 img
和 a
链接。请注意,这是 C# 2.0。
string xml = "<xhtml>" + inputXhtml + "</xhtml>";
XmlDocument node = new XmlDocument();
node.LoadXml(xml);
foreach (XmlElement element in TemplateUtilities.SelectNodes(node, "//html:img[@xlink:href]|//html:a[@xlink:href]"))
{
bool flag = element.LocalName == "img";
lStrCompLinkText = "";
XmlAttributeCollection attributes = element.Attributes;
XmlAttribute namedItem = (XmlAttribute)attributes.GetNamedItem("href", "http://www.w3.org/1999/xlink");
string str2 = namedItem.Value;
Component currentObject = engine.GetObject(str2) as Component;
if (flag)
{
element.SetAttribute("src", str2);
}
else
{
foreach (XmlNode xnode in element.ChildNodes)
{
lStrCompLinkText = lStrCompLinkText + xnode.OuterXml;
}
string attr = ComponentBase.ComponentHelper.ComponentLinkAttributes(element, engine);
string compLink = ComponentBase.ComponentHelper.DisplayPublishedComponentLink(currentObject, lStrCompLinkText, attr, engine, package, pageObject);
attributes.RemoveNamedItem("href", "http://www.tridion.com/ContentManager/5.0");
//Here I want to replace whole element with the compLink
}
attributes.RemoveNamedItem("href", "http://www.w3.org/1999/xlink");
attributes.RemoveNamedItem("type", "http://www.w3.org/1999/xlink");
attributes.RemoveNamedItem("title", "http://www.w3.org/1999/xlink");
}
现在我想用新值 compLink 替换我的元素并添加回输入 HTML
请建议
I have got below code in which I am taking html and then reading all the img
and a
links. Note this is C# 2.0.
string xml = "<xhtml>" + inputXhtml + "</xhtml>";
XmlDocument node = new XmlDocument();
node.LoadXml(xml);
foreach (XmlElement element in TemplateUtilities.SelectNodes(node, "//html:img[@xlink:href]|//html:a[@xlink:href]"))
{
bool flag = element.LocalName == "img";
lStrCompLinkText = "";
XmlAttributeCollection attributes = element.Attributes;
XmlAttribute namedItem = (XmlAttribute)attributes.GetNamedItem("href", "http://www.w3.org/1999/xlink");
string str2 = namedItem.Value;
Component currentObject = engine.GetObject(str2) as Component;
if (flag)
{
element.SetAttribute("src", str2);
}
else
{
foreach (XmlNode xnode in element.ChildNodes)
{
lStrCompLinkText = lStrCompLinkText + xnode.OuterXml;
}
string attr = ComponentBase.ComponentHelper.ComponentLinkAttributes(element, engine);
string compLink = ComponentBase.ComponentHelper.DisplayPublishedComponentLink(currentObject, lStrCompLinkText, attr, engine, package, pageObject);
attributes.RemoveNamedItem("href", "http://www.tridion.com/ContentManager/5.0");
//Here I want to replace whole element with the compLink
}
attributes.RemoveNamedItem("href", "http://www.w3.org/1999/xlink");
attributes.RemoveNamedItem("type", "http://www.w3.org/1999/xlink");
attributes.RemoveNamedItem("title", "http://www.w3.org/1999/xlink");
}
Now I want to replcace my element with the new value compLink and add back to the input HTML
Please suggest
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的评论“//此处我想替换”出现的位置执行以下操作。
从 compLink 的内容创建一个 XML 文档(假设它是 XML)。
使用以下代码从其父级中删除原始元素
导入并将新的 xmlTemp 附加到父级
At the spot where your comment "// Here I want to replace" appears do the following.
Create an XML Document from the contentes of compLink (assuming it's XML).
Remove the original element from it's parent with the following code
Import and append the new xmlTemp to the parent
我使用以下逻辑解决了上述问题:
然后进一步编写了 XSLT,它检查
并替换它,然后我得到实际更新的 xml。I solved my above problem using below logic:
and then further wrote the XSLT which check the
<TCDCode/>
and replaces it and then I get actual updated xml.