在C#中使用XPath使用默认命名空间Canonicalisation(Using Xpath Wi

2019-09-26 19:03发布

我试图以应用C14N变换一些生成的XML。 看来我不能使用LINQ检索节点来执行canonicalisation所以我必须去“老同学”与DOM,但我觉得我属于默认命名空间的犯规。

这里是我的代码示例。

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>

我读过周围地区的其他几个主题和整个这次来到宝石 ,但是它没有解决的问题。

使用XPath Visualiser的显示所需的节点应该进行选择,但我的代码没有任何选择。

Answer 1:

我已经找到了部分答案我的问题。

当一个新的命名空间添加到经理似乎默认的命名空间不能为空字符串。 这是我结束了:

//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结构的正确性,但所有的值已被删除但这是另外一个问题一个问题。



文章来源: Using Xpath With Default Namespace in C# for Canonicalisation