从C#中的节点中删除XML属性

时间:2016-01-05 09:39:28

标签: c# xml attributes

我使用以下代码为EBICS协议生成XML文件:

        XmlWriter xmlw = XmlWriter.Create(Path);
        xmlw.WriteStartDocument();
        xmlw.WriteStartElement("ebicsNoPubKeyDigestsRequest", "http://www.ebics.org/H003");
        xmlw.WriteStartAttribute("Revision");
        xmlw.WriteValue(1);
        xmlw.WriteEndAttribute();//attr:Revision
        xmlw.WriteStartAttribute("Version");
        xmlw.WriteValue("H003");
        xmlw.WriteEndAttribute();//attr:Version
        xmlw.WriteStartElement("header");
        xmlw.WriteStartAttribute("authenticate");
        xmlw.WriteValue(true);
        xmlw.WriteEndAttribute();//attr:authenticate
        xmlw.WriteStartElement("static");
        xmlw.WriteStartElement("HostID");
        xmlw.WriteValue(HostID);
        xmlw.WriteEndElement();//HostID
        xmlw.WriteStartElement("Nonce");
        xmlw.WriteValue(GlobalControl.GenereNonce());
        xmlw.WriteEndElement();//Nonce
        xmlw.WriteStartElement("Timestamp");
        xmlw.WriteValue("" + DateTime.Now.Year.ToString() + "-" + String.Format("{0:00}", DateTime.Now.Month) + "-" + String.Format("{0:00}", DateTime.Now.Day) + "T" + String.Format("{0:00}", DateTime.Now.Hour) + ":" + String.Format("{0:00}", DateTime.Now.Minute) + ":" + String.Format("{0:00}", DateTime.Now.Second) + "." + DateTime.Now.Millisecond + "+02:00");
        xmlw.WriteEndElement();//Timestamp
        xmlw.WriteStartElement("PartnerID");
        xmlw.WriteValue(PartnerID);
        xmlw.WriteEndElement();//PartnerID
        xmlw.WriteStartElement("UserID");
        xmlw.WriteValue(UserID);
        xmlw.WriteEndElement();//UserID
        xmlw.WriteStartElement("OrderDetails");
        xmlw.WriteStartElement("OrderType");
        xmlw.WriteValue("HPB");
        xmlw.WriteEndElement();//OrderType
        xmlw.WriteStartElement("OrderAttribute");
        xmlw.WriteValue("DZHNN");
        xmlw.WriteEndElement();//OrderAttribute
        xmlw.WriteEndElement();//OrderDetails
        xmlw.WriteStartElement("SecurityMedium");
        xmlw.WriteValue("0000");
        xmlw.WriteEndElement();//SecurityMedium
        xmlw.WriteEndElement();//static
        xmlw.WriteStartElement("mutable");
        xmlw.WriteEndElement();
        xmlw.WriteEndElement();//header
        xmlw.WriteEndElement();//ebicsNoPubKeyDigestsRequest
        xmlw.Close();
        XmlDocument docHPB = new XmlDocument();
        docHPB.Load(Path);
        docHPB.DocumentElement.SetAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");
        docHPB.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        docHPB.DocumentElement.SetAttribute("xsi:schemaLocation", "http://www.ebics.org/H003 http://www.ebics.org/H003/ebics_keymgmt_request.xsd");
        docHPB.Save(Path);
        SignXml(Path, CertificatAuthentication, PasswordCertificats);
        docHPB.Load(Path);
        XmlElement body = docHPB.CreateElement("body");
        docHPB.DocumentElement.AppendChild(body);
        docHPB.Save(Path);

我得到以下结果:

<?xml version="1.0" encoding="utf-8"?>
<ebicsNoPubKeyDigestsRequest Revision="1" Version="H003" xmlns="http://www.ebics.org/H003" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.ebics.org/H003 http://www.ebics.org/H003/ebics_keymgmt_request.xsd">
  <header authenticate="true">
    <static>
      <HostID>EBIXQUAL</HostID>
      <Nonce>A33A2AE12D1FCDEBEB0623848242680A</Nonce>
      <Timestamp>2016-01-05T10:27:08.746+02:00</Timestamp>
      <PartnerID>SAPSE</PartnerID>
      <UserID>ERTYU</UserID>
      <OrderDetails>
        <OrderType>HPB</OrderType>
        <OrderAttribute>DZHNN</OrderAttribute>
      </OrderDetails>
      <SecurityMedium>0000</SecurityMedium>
    </static>
    <mutable />
  </header>
  <AuthSignature xmlns="">
    <ds:SignedInfo>
      <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
      <ds:Reference URI="#xpointer(//*[@authenticate='true'])">
        <ds:Transforms>
          <ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
        </ds:Transforms>
        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
        <ds:DigestValue>hYi/lmjXm8J4LWtbPqsa8e9mfHlWd1WJ8EEIFnCDJhM=</ds:DigestValue>
      </ds:Reference>
    </ds:SignedInfo>
    <ds:SignatureValue>JWf3WjHTeg1Z5Ix2euMhD/S7zeSn7pRV3+uiD8IJyHQxtQbxY84kokGUoii7lHVQHx5QcKtPTtAeQQZvgODtapfD/x12KeDTPOSw/9KSN5NwA6RdxAYwukQka73u8xNLWT5tfnuFNU3i6DOYf7MA/GeCYh0GLDFkFyOz6GjwD3iPIDOzyM16s9J4G+XtLOqwFrotQQF/F+akMf+DWWCE6QUWQn/HfZRLKi78g9nzz+Eom4Y041k6zWjlA8w/H31vCslgLy9BANO/GSXsh9uQEf7o5OHdfhXD5dxkbvD6+QQinIfulK4Dnb0xmguL3MxItWCIcE8vHuUGQwbI0/oWaA==</ds:SignatureValue>
  </AuthSignature>
  <body xmlns="" />
</ebicsNoPubKeyDigestsRequest>

但我的结果中有两个错误,节点xmlnsAuthSignature上有body属性,我想删除它们,但我不知道如何才能执行此操作。 我已经尝试过这样的事了:

docHPB.DocumentElement.ChildNodes[1].Attributes.RemoveAll();

但它不起作用

有人能帮助我吗?

提前谢谢

1 个答案:

答案 0 :(得分:1)

结果有&#34; xmlns&#34;因为SetAttribute

DocumentElement
 docHPB.DocumentElement.SetAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");
 docHPB.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

请参考这篇文章,希望对你有帮助! XmlDocument CreateElement without xmlns under a prefixed element