XML rubics cube

I’m beginning to think this is an unsolvable rubies cube. I’ve moved things around so much, I can’t remember what I’ve tried and what I haven’t.

Here is the first line in the XML file that I need to generate:

<n1:Form109495BTransmittalUpstream
xmlns=“urn:us:gov:treasury:irs:common”
xmlns:air6.2=“urn:us:gov:treasury:irs:ext:aca:air:6.2”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
xmlns:n1=“urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage”
xsi:schemaLocation=“urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage IRS-Form1094-1095BTransmitterUpstreamMessage.xsd”>

Here is what I’m getting:

<n1:Form109495BTransmittalUpstream
xmlns:n1=“xmlns=urn:us:gov:treasury:irs:common”
xmlns:air6.2=“urn:us:gov:treasury:irs:ext:aca:air:6.2”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=“urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage IRS-Form1094-1095BTransmitterUpstreamMessage.xsd”/>

And here is my code:

XmlDoc = New XmlDocument
XmlDoc.AppendChild XmlDoc.CreateElement("xmlns=urn:us:gov:treasury:irs:common","n1:Form109495BTransmittalUpstream")
XmlDoc.DocumentElement.SetAttribute("xmlns:air6.2","urn:us:gov:treasury:irs:ext:aca:air:6.2")
XmlDoc.DocumentElement.SetAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
XmlDoc.DocumentElement.SetAttribute("xmlns:n1","urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage")
XmlDoc.DocumentElement.SetAttribute("xsi:schemaLocation","urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage IRS-Form1094-1095BTransmitterUpstreamMessage.xsd")

Problems:

2nd line - “xmlmns=” is on the right side and inside the quotes and shouldn’t be there.

I’m can’t get the 5th line - xmlns:n1=“urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage” - to print even though I’m setting the attribute to include it.

I hope someone out there understands this better than I do and will chime in.

Kem where are you?

A couple of things I noticed while playing around…first, you can get rid of the extra “xmlns=” in line 2 if you change your CreateElement line to:
XmlDoc.AppendChild XmlDoc.CreateElement(“urn:us:gov:treasury:irs:common”,“n1:Form109495BTransmittalUpstream”)

But this (and your code) is making line 2 “xmlns:n1” instead of just “xmlns”. I don’t do namespace stuff, but you shouldn’t have just plain “xmlns”, right? Then later on for line 5 you’re trying to set xmlns:n1 again, right? If you change one of those "n1"s to something else, they both print, so they don’t like competing with each other.

Bill - Thank you for you’re reply. I was beginning to think I was alone in the wilderness with this problem.

I made a little change to my code this morning and this is what I ended up with. I’ve just got one line out of place.

This is what my code generates now - everything is correct except line 2 needs to get moved to the 5th line.

<n1:Form109495BTransmittalUpstream
xmlns:n1=“urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage”
xmlns=“urn:us:gov:treasury:irs:common”
xmlns:air6.2=“urn:us:gov:treasury:irs:ext:aca:air:6.2”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=“urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage IRS-Form1094-1095BTransmitterUpstreamMessage.xsd”/>

This is what I need to end up with.

<n1:Form109495BTransmittalUpstream
xmlns=“urn:us:gov:treasury:irs:common”
xmlns:air6.2=“urn:us:gov:treasury:irs:ext:aca:air:6.2”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
xmlns:n1=“urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage”
xsi:schemaLocation=“urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage IRS-Form1094-1095BTransmitterUpstreamMessage.xsd”>

This is my code:

XmlDoc.AppendChild XmlDoc.CreateElement("urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage","n1:Form109495BTransmittalUpstream")
XmlDoc.DocumentElement.SetAttribute("xmlns","urn:us:gov:treasury:irs:common")
XmlDoc.DocumentElement.SetAttribute("xmlns:air6.2","urn:us:gov:treasury:irs:ext:aca:air:6.2")
XmlDoc.DocumentElement.SetAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
XmlDoc.DocumentElement.SetAttribute("xsi:schemaLocation","urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage IRS-Form1094-1095BTransmitterUpstreamMessage.xsd")

The first line and the last line of my code are wrapping.

I don’t see a way to change the order - I tried removing the xmlns:n1 attribute node so I could add it later, but it won’t let me (I get an exception). I assume it’s because it is protecting you from doing something silly like removing it and forgetting to put it back.

So if you HAVE to have it in the order you want, I’d probably generate it as is, get the string, manipulate the order, then save it that way. Kind of a pain though.

Does the order really matter? It’s not supposed to.

Update - For now, I’m not worried about the order of the namespace specification in the root node, but the rest of the XML file is generating just perfect so I think I’m good for now. Thanks for all the replies.

If I find that the order matters in the root node, I will try Bill’s suggestion of just editing the XML code as a text file.

This is for an xml document that will be submitted to the IRS and we don’t have any live XML file testing available yet, but that will happen this fall.