0

I using the following code to generate an XML File for the EBICS protocol :

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

And I get the following result :

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

But there is two errors in my result, there is xmlns attribute on the node AuthSignature and body and I want to remove them but I have no idea how I can do this. I already try something like this :

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

But It doesn't work

Can someone help me ?

Thank you in advance

2
  • So what is the objective, to create a certain XML using XML namespaces? Your code is an odd mixture of XmlWriter and XmlDocument, is that because you failed to create the wanted output with one of them, or do you have any good reason to mix them? In general if you need to create XML with namespaces then you need to create elements in the right namespace from the beginning and not try to remove namespace declarations or undeclarations later. Commented Jan 5, 2016 at 10:03
  • @MartinHonnen I need to create my XML file, sign it without the body node and reopen it to add it. Commented Jan 5, 2016 at 10:08

1 Answer 1

1

Result has "xmlns" because you SetAttribute for 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");

Please refer this post, hope it help! XmlDocument CreateElement without xmlns under a prefixed element

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.