2011-08-03 18 views
1

我有related post询问如何使用XPath语句从XmlDocument中选择节点。XML规范化返回转换输出中的空元素

我可以让SelectNodes工作的唯一方法是创建一个非默认命名空间“x”,然后显式引用XPath语句中的节点。

虽然这可以工作并为我提供了一个节点列表,但是规范化却无法向输出中的所选节点生成任何内容。

我试过使用XmlDsigExcC14NTransform并指定命名空间,但是这会产生相同的输出。

下面是(在我related post使用XML)生成的XML输出的一个例子:

<Applications xmlns="http://www.myApps.co.uk/"> 
    <Application> 
    <ApplicantDetails> 
     <Title> 
     </Title> 
     <Forename> 
     </Forename> 
     <Middlenames> 
     <Middlename> 
     </Middlename> 
     </Middlenames> 
     <PresentSurname> 
     </PresentSurname> 
     <CurrentAddress> 
     <Address> 
      <AddressLine1> 
      </AddressLine1> 
      <AddressLine2> 
      </AddressLine2> 
      <AddressTown> 
      </AddressTown> 
      <AddressCounty> 
      </AddressCounty> 
      <Postcode> 
      </Postcode> 
      <CountryCode> 
      </CountryCode> 
     </Address> 
     <ResidentFromGyearMonth> 
     </ResidentFromGyearMonth> 
     </CurrentAddress> 
    </ApplicantDetails> 
    </Application> 
    <Application> 
    <ApplicantDetails> 
     <Title> 
     </Title> 
     <Forename> 
     </Forename> 
     <Middlenames> 
     <Middlename> 
     </Middlename> 
     </Middlenames> 
     <PresentSurname> 
     </PresentSurname> 
     <CurrentAddress> 
     <Address> 
      <AddressLine1> 
      </AddressLine1> 
      <AddressLine2> 
      </AddressLine2> 
      <AddressTown> 
      </AddressTown> 
      <AddressCounty> 
      </AddressCounty> 
      <Postcode> 
      </Postcode> 
      <CountryCode> 
      </CountryCode> 
     </Address> 
     <ResidentFromGyearMonth> 
     </ResidentFromGyearMonth> 
     </CurrentAddress> 
    </ApplicantDetails> 
    </Application> 
</Applications> 

回答

2

另一个StackOverflow的用户已经有了similar problem here

与这个新的代码打的时候,我发现,结果会因您将节点传递到LoadInput方法的方式而有所不同。实现下面的代码工作。

我仍然好奇,为什么它的作品的一种方式,而不是另一个,但会留下,以备不时之需

static void Main(string[] args) 
{ 
    string path = @"..\..\TestFiles\Test_1.xml"; 
    if (File.Exists(path) == true) 
    { 
     XmlDocument xDoc = new XmlDocument(); 
     xDoc.PreserveWhitespace = true; 
     using (FileStream fs = new FileStream(path, FileMode.Open)) 
     { 
      xDoc.Load(fs); 
     } 

     //Instantiate an XmlNamespaceManager object. 
     System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xDoc.NameTable); 

     //Add the namespaces used to the XmlNamespaceManager. 
     xmlnsManager.AddNamespace("x", "http://www.myApps.co.uk/"); 

     // Create a list of nodes to have the Canonical treatment 
     XmlNodeList nodeList = xDoc.SelectNodes("/x:ApplicationsBatch/x:Applications|/x:ApplicationsBatch/x:Applications//*", xmlnsManager); 

     //Initialise the stream to read the node list 
     MemoryStream nodeStream = new MemoryStream(); 
     XmlWriter xw = XmlWriter.Create(nodeStream); 
     nodeList[0].WriteTo(xw); 
     xw.Flush(); 
     nodeStream.Position = 0; 

     // Perform the C14N transform on the nodes in the stream 
     XmlDsigC14NTransform transform = new XmlDsigC14NTransform(); 
     transform.LoadInput(nodeStream); 

     // use a new memory stream for output of the transformed xml 
     // this could be done numerous ways if you don't wish to use a memory stream 
     MemoryStream outputStream = (MemoryStream)transform.GetOutput(typeof(Stream)); 
     File.WriteAllBytes(@"..\..\TestFiles\CleanTest_1.xml", outputStream.ToArray()); 
    } 
}