1

XPathステートメントを使用してXmlDocumentからノードを選択する方法を尋ねる関連記事があります。

SelectNodesを機能させる唯一の方法は、デフォルト以外の名前空間 "x"を作成し、XPathステートメントでノードを明示的に参照することでした。

これは機能し、ノードリストを提供しますが、正規化では、出力で選択したノードにコンテンツを生成できません。

XmlDsigExcC14NTransformを使用して名前空間を指定しようとしましたが、同じ出力が生成されます。

以下は、(関連する投稿の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>
4

1 に答える 1

2

別のStackOverflowユーザーがここで同様の問題を抱えています

この新しいコードを試してみると、ノードを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());
    }
}
于 2011-08-04T15:03:01.937 に答える