XML 规范化在转换后的输出中返回空元素

发布于 2024-11-27 10:21:36 字数 2446 浏览 4 评论 0原文

我有一篇相关帖子询问如何选择节点使用 XPath 语句从 XmlDocument 中获取。

让 SelectNodes 工作的唯一方法是创建一个非默认命名空间“x”,然后在 XPath 语句中显式引用节点。

虽然这有效并为我提供了一个节点列表,但规范化无法在输出中为我选择的节点生成任何内容。

我尝试使用 XmlDsigExcC14NTransform 并指定命名空间,但这会产生相同的输出。

下面是生成的 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>

I have a related post asking how to select nodes from an XmlDocument using an XPath statement.

The only way I could get the SelectNodes to work was to create a non default namespace "x" and then explicitly reference the nodes in the XPath statement.

Whilst this works and provides me with a node list, the canonicalization then fails to produce any content to my selected nodes in the output.

I've tried using XmlDsigExcC14NTransform and specifying the namespace but this produces the same output.

Below is an example of the xml output produced (using the XML in my related post):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平定天下 2024-12-04 10:21:36

的问题

另一位 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());
    }
}

Another StackOverflow user has had a similar problem here

Playing around with this new code, I found that the results differ depending upon how you pass the nodes into the LoadInput method. Implementing the code below worked.

I'm still curious as to why it works one way and not another but will leave that for a rainy day

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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文