2011-08-02 29 views
1

我試圖將C14N轉換應用於某些生成的XML。看來我不能使用LINQ來檢索節點來執行規範化,所以我必須用DOM去「舊學校」,但我認爲我正在陷入默認命名空間。在C#中使用帶默認命名空間的Xpath進行規範化

這是我的代碼示例。

static void Main(string[] args) 
{ 
    XmlDocument xDoc = new XmlDocument(); 

    // Load some test xml 
    string path = @"..\..\TestFiles\Test_1.xml"; 
    if (File.Exists(path) == true) 
    { 
     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 in books.xml to the XmlNamespaceManager. 
    xmlnsManager.AddNamespace("", "http://www.myApps.co.uk/"); 

    // Create a list of nodes to have the Canonical treatment 
     //Execute the XPath query using the SelectNodes method of the XmlDocument. 
     //Supply the XmlNamespaceManager as the nsmgr parameter. 
     //The matching nodes will be returned as an XmlNodeList. 
    XmlNodeList nodeList = xDoc.SelectNodes("/ApplicationsBatch/Applications|/ApplicationsBatch/Applications//*", xmlnsManager); 

    // Perform the C14N transform on the data 
    XmlDsigC14NTransform transform = new XmlDsigC14NTransform(); 

    transform.LoadInput(nodeList); 
    MemoryStream ms = (MemoryStream)transform.GetOutput(typeof(Stream)); 

    File.WriteAllBytes(@"..\..\TestFiles\ModifiedTest_1", ms.ToArray()); 
} 

我的XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<ApplicationsBatch xmlns="http://www.myApps.co.uk/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <MessageHeader> 
    <MessageID>00000003</MessageID> 
    <Body>11223344556</Body> 
    <Timestamp>2011-08-02T09:00:00</Timestamp> 
    <MessageCheck>?</MessageCheck> 
    </MessageHeader> 
    <Applications> 
    <Application> 
     <ApplicantDetails> 
     <Title>MR</Title> 
     <Forename>HOMER</Forename> 
     <Middlenames> 
      <Middlename></Middlename> 
     </Middlenames> 
     <PresentSurname>SIMPSON</PresentSurname> 
     <CurrentAddress> 
      <Address> 
      <AddressLine1>ADDRESS LINE1</AddressLine1> 
      <AddressLine2>ADDRESS LINE2</AddressLine2> 
      <AddressTown>ADDRESS Town</AddressTown> 
      <AddressCounty>COUNTY</AddressCounty> 
      <Postcode>POST CODE</Postcode> 
      <CountryCode>GB</CountryCode> 
      </Address> 
      <ResidentFromGyearMonth>2007-01</ResidentFromGyearMonth> 
     </CurrentAddress> 
     </ApplicantDetails> 
    </Application> 
    <Application> 
     <ApplicantDetails> 
     <Title>MR</Title> 
     <Forename>BART</Forename> 
     <Middlenames> 
      <Middlename></Middlename> 
     </Middlenames> 
     <PresentSurname>SIMPSON</PresentSurname> 
     <CurrentAddress> 
      <Address> 
      <AddressLine1>ADDRESS LINE1</AddressLine1> 
      <AddressLine2>ADDRESS LINE2</AddressLine2> 
      <AddressTown>ADDRESS Town</AddressTown> 
      <AddressCounty>COUNTY</AddressCounty> 
      <Postcode>POST CODE</Postcode> 
      <CountryCode>GB</CountryCode> 
      </Address> 
      <ResidentFromGyearMonth>2007-01</ResidentFromGyearMonth> 
     </CurrentAddress> 
     </ApplicantDetails> 
    </Application> 
    </Applications> 
</ApplicationsBatch> 

我讀過周圍地區的其他幾個主題和跨越這個Gem來了,但它不是解決了這個問題。

使用XPath Visualiser顯示應選擇所需的節點,但我的代碼未能選擇任何節點。

回答

0

我發現我的問題的部分答案。

當新的名稱空間添加到管理器時,看起來默認名稱空間不能是空字符串。 這是我結束了:

//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/"); 

然後我需要修改的XPath來反映這樣的命名空間標識:

// Create a list of nodes to have the Canonical treatment 
    //Execute the XPath query using the SelectNodes method of the XmlDocument. 
    //Supply the XmlNamespaceManager as the nsmgr parameter. 
    //The matching nodes will be returned as an XmlNodeList. 
XmlNodeList nodeList = xDoc.SelectNodes("/x:ApplicationsBatch/x:Applications|/x:ApplicationsBatch/x:Applications//*", xmlnsManager); 

節點正在選擇並準備改造...儘管這返回了XML的正確結構,但是所有的值都被刪除了,但這對於另一個問題是一個問題。

+0

你的意思是說你不能指定默認的名稱空間用於xpath。這很有意義,因爲xpath將覆蓋具有多個前綴的多個節點,更重要的是必須處理具有空名稱空間的節點,這看起來像xpath中的默認名稱空間。在XML中,您可以使用上下文來幫助確定它是默認名稱空間還是空名稱空間。 –

相關問題