用新值替换整个 XmlElement

发布于 2024-12-17 04:56:07 字数 1622 浏览 0 评论 0原文

我有下面的代码,我在其中获取 html,然后读取所有 imga 链接。请注意,这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_失温 2024-12-24 04:56:07

在您的评论“//此处我想替换”出现的位置执行以下操作。

从 compLink 的内容创建一个 XML 文档(假设它是 XML)。

XmlDocument xmlTemp = new XmlDocument();
xmlTemp.loadXml( compLink );

使用以下代码从其父级中删除原始元素

XmlNode ndParent = element.ParentNode;
ndParent.RemoveChild( element);

导入并将新的 xmlTemp 附加到父级

XmlNode ndImport = ndParent.OwnerDocument.ImportNode( xmlTemp.documentElement, true );
ndParent.AppendChild( ndImport.CloneNode( true ) );

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).

XmlDocument xmlTemp = new XmlDocument();
xmlTemp.loadXml( compLink );

Remove the original element from it's parent with the following code

XmlNode ndParent = element.ParentNode;
ndParent.RemoveChild( element);

Import and append the new xmlTemp to the parent

XmlNode ndImport = ndParent.OwnerDocument.ImportNode( xmlTemp.documentElement, true );
ndParent.AppendChild( ndImport.CloneNode( true ) );
三寸金莲 2024-12-24 04:56:07

我使用以下逻辑解决了上述问题:

XmlDocument lObjTCDCodeDom = new XmlDocument();
lObjTCDCodeDom.LoadXml("<TCDCode/>");
lObjTCDCodeDom.DocumentElement.InnerText = compLink;
element.ParentNode.ReplaceChild(node.ImportNode(lObjTCDCodeDom.DocumentElement, true), element);

然后进一步编写了 XSLT,它检查 并替换它,然后我得到实际更新的 xml。

I solved my above problem using below logic:

XmlDocument lObjTCDCodeDom = new XmlDocument();
lObjTCDCodeDom.LoadXml("<TCDCode/>");
lObjTCDCodeDom.DocumentElement.InnerText = compLink;
element.ParentNode.ReplaceChild(node.ImportNode(lObjTCDCodeDom.DocumentElement, true), element);

and then further wrote the XSLT which check the <TCDCode/> and replaces it and then I get actual updated xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文