0

I am having trouble generating an xml root. I have to match this structure as the elements of the xml use the prefixs throughout.

<ShipmentReceiptNotification
xmlns="urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.02"
xmlns:dacc="urn:rosettanet:specification:domain:Procurement:AccountClassification:xsd:codelist:01.03"
xmlns:dbpq="urn:rosettanet:specification:domain:Procurement:BookPriceQualifier:xsd:codelist:01.04"
xmlns:dccc="urn:rosettanet:specification:domain:Procurement:CreditCardClassification:xsd:codelist:01.03"
xmlns:dcrt="urn:rosettanet:specification:domain:Procurement:CustomerType:xsd:codelist:01.03"
..\..\XML\Interchange\ShipmentReceiptNotification_02_02.xsd">

if I do something like

XmlNode ShipmentReceiptNotification0Node = xmlDoc.CreateElement("ShipmentReceiptNotification", "xmlns=\"urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.02\"xmlns:dacc=\"urn:rosettanet:specification:domain:Procurement:AccountClassification:xsd:codelist:01.03\"");

I get

-ShipmentReceiptNotification xmlns="xmlns="urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.02"xmlns:dacc=&

quot;urn:rosettanet:specification:domain:Procurement:AccountClassification:xsd:codelist:01.03"">

1
  • 1
    The namespace is the part after xmlns=". It's just urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.02, for instance. Also, don't worry about putting them all in the root. Just create the correct elements and attributes, in the correct namespaces. Also, you'll find LINQ to XML much easier. Commented Mar 8, 2013 at 17:07

1 Answer 1

1

The second argument of CreateElement accepts the URI of the namespace that the element being created, that is ShipmentReceiptNotification, belongs to. Not the whole bunch of xmlns attributes. This code:

XmlElement e = xmlDoc.CreateElement(
    "ShipmentReceiptNotification",
    "urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.02");

Produces this XML:

<ShipmentReceiptNotification 
    xmlns="urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.02" />

To produce what you want, you need to add attributes to the element. Like this:

XmlElement e = xmlDoc.CreateElement("ShipmentReceiptNotification");
e.SetAttribute("xmlns", "urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.02");
e.SetAttribute("xmlns:dacc", "urn:rosettanet:specification:domain:Procurement:AccountClassification:xsd:codelist:01.03");

Produces this XML:

<ShipmentReceiptNotification 
    xmlns="urn:rosettanet:specification:interchange:ShipmentReceiptNotification:xsd:schema:02.02" 
    xmlns:dacc="urn:rosettanet:specification:domain:Procurement:AccountClassification:xsd:codelist:01.03" />

Note that this is the “manual” way. You should play with XmlNamespaceManager to do it “right”. However, that may be a bit more complex task which need not be necessary for your scenario.

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

5 Comments

I believe I need to use the xmlnamespacemanager as I can add all the attributes but when I create elements with prefixs the prefix as defined in the attributes will not display in the ending xmldocument.
Yes. Just don't be surprised that XmlDocument won't accept any XmlNamespaceManager instance anywhere; they both just share the same XmlNameTable instance.
do I have to use xelement ?
In the latest versions of .NET there are two parallel XML libraries: XmlDocument et al. and XDocument et al. The later being more recent, and LINQ-enabled. Which one to use depends on your use case and also on the mere fact if you already have a substantial amount of code utilizing one or another.
I would prefer to stick to xmldocument but I can't get any prefixs for later elements to work with the attribute method for root node namespaces

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.