2009-03-02 4 views

답변

4

XmlDocument를 사용하면 두 번째 문서로 노드를 가져와야합니다.

XmlDocument doc = new XmlDocument(); 
    XmlElement root = (XmlElement)doc.AppendChild(doc.CreateElement("root")); 
    XmlNodeList list = // your query 
    foreach (XmlElement child in list) 
    { 
     root.AppendChild(doc.ImportNode(child, true)); 
    } 
+0

이 내가 갔다 답변입니다. –

0

나는 이것을 실제로하는 방법을 생각해 보았지만 매우 우아하지는 않다.

나는 그것이 세련입니다하지만 난 그게 작동 할 수 있습니다 생각 말했듯이 ...

XmlNodeList의 된 XMLNodes 각각의 OuterXml을 결합하기 위해 모두 StringBuilder를 사용합니다. 나는 다른 제안을 주시면 감사하겠습니다 ...

1

이것은 하나의 XML 문서를 다른 문서 (또는 HTML 또는 텍스트)로 변환하기위한 효율적이고 강력한 도구 인 XSLT를 사용하는 일반적인 이유입니다.

는 여기에 XSLT 변환 및 콘솔에 결과를 보내 수행하기 위해 최소한의 프로그램입니다 :

using System; 
using System.Xml; 
using System.Xml.Xsl; 

namespace XsltTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      XslCompiledTransform xslt = new XslCompiledTransform(); 
      xslt.Load("test.xslt"); 

      XmlWriter xw = XmlWriter.Create(Console.Out); 
      xslt.Transform("input.xml", xw); 
      xw.Flush(); 
      xw.Close(); 

      Console.ReadKey(); 
     } 
    } 
} 

가 여기에 프로그램 디렉토리에 test.xslt에 저장되는 실제 XSLT,입니다. 매우 간단합니다 : 최상위 요소의 이름이 input 인 입력 문서가 있으면 output 요소가 만들어지고속성이 true으로 설정된 모든 하위 요소에 복사됩니다.

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/input"> 
     <output> 
     <xsl:apply-templates select="*[@value='true']"/> 
     </output> 
    </xsl:template> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

그리고 여기가 input.xml입니다 : 결국

<?xml version="1.0" encoding="utf-8" ?> 
<input> 
    <element value="true"> 
    <p>This will get copied to the output.</p> 
    <p>Note that the use of the identity transform means that all of this content 
    gets copied to the output simply because templates were applied to the 
    <em>element</em> element. 
    </p> 
    </element> 
    <element value="false"> 
    <p>This, on the other hand, won't get copied to the output.</p> 
    </element> 
</input> 
+0

그것은 필자가 동적으로 필터링하고 있기 때문에 사전에 필터링해야하는 것을 모릅니다. +1 멋진 XSLT입니다. –

+0

XSLT를 사용하여 동적으로 필터링하는 방법은 많습니다. 인수를 변환에 전달할 수 있습니다. 이러한 인수에는 변환이 읽고 적용하는 필터링 기준이 포함될 수 있습니다. –

관련 문제