English 中文(简体)
重置具有新价值的XmlElement
原标题:Replacing Whole XmlElement with new value
  • 时间:2011-11-22 10:06:53
  •  标签:
  • c#
  • xml
  • c#-2.0

我已读到以下代码,然后读到所有<代码>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");
}

现在,我想用新的价值“红外线”来补充我的内容,并补充对超文本投入的投入。

请建议

最佳回答

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

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

然后,还写了XSLT,对<代码><TCDCode/>进行了核对,并替换了该编码,然后,我收到了实际更新的xml。

问题回答

At the spot where your comment "// Here I want to replace" appears do the following.

建立XML 摘自ComLink内容的文件(概述XML)。

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

将原元素从母体移除,代号如下:

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

进口新锡尔雅帝国并将其贴在母公司

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签