0

I am bad at xml understanding, please help me this is xml that I need

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:ENRC-IDOC_939_DigiDocs:idm">
  <soapenv:Header />
  <soapenv:Body>
    <urn:mt_digidocs_fees>

This is my code

XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration( "1.0", "UTF-8", null );
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);
XmlElement envelope = doc.CreateElement("soapenv","Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
XmlAttribute urn = doc.CreateAttribute("xmlns","urn", "http://www.w3.org/2000/xmlns/");
urn.Value = "urn:ENRC-IDOC_939_DigiDocs:idm";  
envelope.Attributes.SetNamedItem(urn);
doc.AppendChild(envelope);
XmlNode soapenvheader = doc.CreateElement("soapenv", "Header", doc.DocumentElement.NamespaceURI);
envelope.AppendChild(soapenvheader);
XmlNode body = doc.CreateElement("soapenv", "Body", doc.DocumentElement.NamespaceURI);
envelope.AppendChild(body);  
XmlElement mt_digidocs_fees = doc.CreateElement("mt_digidocs_fees");
mt_digidocs_fees.Prefix = "urn";
body.AppendChild(mt_digidocs_fees);

and this is what I get

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:urn="urn:ENRC-IDOC_939_DigiDocs:idm" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header />
  <soapenv:Body>
    <mt_digidocs_fees>

So there last Element's name does not contain prefix and namespace addresses are in different order

Thanks in advance

Liam
  • 27,717
  • 28
  • 128
  • 190
Bakytzhan
  • 15
  • 2
  • 1
    `mt_digidocs_fees = doc.CreateElement("mt_digidocs_fees", "urn:ENRC-IDOC_939_DigiDocs:idm");` ? – Selvin Jul 16 '21 at 13:46
  • Why don't you just use a SOAP library to do this for you? – Liam Jul 16 '21 at 13:47
  • @Liam Could you explain a bit more? I just got the xml and was asked to send my data in this format. Seems like I have to read about SOAP a little bit – Bakytzhan Jul 19 '21 at 09:04
  • SOAP (Simple Object Access Protocol) [is a standard](https://www.w3.org/TR/2000/NOTE-SOAP-20000508/) albeit quite an old one. Visual studio [supports this out of the box](https://stackoverflow.com/questions/3100458/soap-client-in-net-references-or-examples) – Liam Jul 19 '21 at 09:06
  • Or it used to, I'm not sure if it still does. As mentioned SOAP is quite old now – Liam Jul 19 '21 at 09:07
  • [WCF](https://learn.microsoft.com/en-us/dotnet/framework/wcf/whats-wcf) is the standard framework for consuming SOAP requests – Liam Jul 19 '21 at 09:09

1 Answers1

0

I like using Xml Linq like this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string ident = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:ENRC-IDOC_939_DigiDocs:idm\"></soapenv:Envelope>";
            XDocument doc = XDocument.Parse(ident);
            XElement envelope = doc.Root;
            XNamespace nsUrn = envelope.GetNamespaceOfPrefix("urn");
            XNamespace nsSoapenv = envelope.GetNamespaceOfPrefix("soapenv");

            XElement header = new XElement(nsSoapenv + "Header");
            envelope.Add(header);
            XElement body = new XElement(nsSoapenv + "Body");
            envelope.Add(body);
            XElement fees = new XElement(nsUrn + "mt_digidocs_fees");
            body.Add(fees);
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20