2011-08-03 69 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()); 
    } 
}