2011-08-02 5 views
1

生成されたXMLにC14N変換を適用しようとしています。正規化を実行するノードを取得するためにLINQを使用することはできないようですので、DOMで「古い学校」に行かなければなりませんが、私はデフォルトの名前空間に惑わされると思います。Canonicalisationのための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:私は地域の周りにいくつかの他のトピックを読んで、このGemに出くわしたが、それは問題を解決していないました

<?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> 

XPathビジュアライザを使用すると、必要なノードを選択する必要がありますが、コードでは選択できません。

答えて

0

私は私の問題の部分的な答えを見つけました。

新しい名前空間がマネージャに追加されると、デフォルトの名前空間を空の文字列にすることはできません。私は、このような名前空間識別子を反映するために、XPathを変更する場合は、必要に応じ

//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 
    //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の中には、それがデフォルトネームスペースかヌルネームスペースかを判断するのに役立つコンテキストがあります。 –

関連する問題