簡體   English   中英

從C#中的節點刪除XML屬性

[英]Remove XML attribute from a node in C#

我使用以下代碼為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>

但是我的結果有兩個錯誤,節點AuthSignaturebody上有xmlns屬性,我想刪除它們,但是我不知道該怎么做。 我已經嘗試過這樣的事情:

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

但這不起作用

有人能幫我嗎 ?

先感謝您

結果具有“ xmlns”,因為您為DocumentElement SetAttribute

 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在前綴元素下沒有xmlns

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM