VB NET XML

Hi,

I have been asked to add the following line to my XML file by our customer but I am unsure how to or what exactly it is? I know one part is referencing a namespace...

<ns0:IMG_DOCUMENT xmlns:ns0="urn://nagelgroup.com">

The XML document currently looks like this:

<?xml version="1.0" encoding="utf-8"?>
<DocumentProcessorGroups>
<group>
  <PageCount>1</PageCount>
  <FileCount>1</FileCount>
  <FileName>C:\12345</FileName>
  <Barcode>1223458421</Barcode>
</group>
<Variables>
  <NUMMER></NUMMER>
  <DATUM></DATUM>
  <KUNDENNR></KUNDENNR>
  <KUNDENNAME></KUNDENNAME>
  <BUKREIS></BUKREIS>
</Variables>
</DocumentProcessorGroups>


But need to look like this:
<?xml version="1.0" encoding="utf-8"?><ns0:IMG_DOCUMENT xmlns:ns0="urn://nagelgroup.com">
<DocumentProcessorGroups>
<group>
  <PageCount>1</PageCount>
  <FileCount>1</FileCount>
  <FileName>C:\12345</FileName>
  <Barcode>1223458421</Barcode>
</group>
<Variables>
  <NUMMER></NUMMER>
  <DATUM></DATUM>
  <KUNDENNR></KUNDENNR>
  <KUNDENNAME></KUNDENNAME>
  <BUKREIS></BUKREIS>
</Variables>
</DocumentProcessorGroups>

My current code is:

Dim xml As XDocument = New XDocument(
                    New XDeclaration("1.0", "UTF-8", String.Empty),
                    New XCData("ns0:IMG_DOCUMENT xmlns:ns0=""urn://nagelgroup.com"""),
                    New XElement("DocumentProcessorGroups",
                        New XElement("group",
                            New XElement("PageCount", sPageCount),
                            New XElement("FileCount", "1"),
                            New XElement("FileName", Path.GetFileNameWithoutExtension(sTargetPDFFullFilename)),
                            New XElement("Barcode", sBarcode)),
                                New XElement("Variables",
                                New XElement("NUMMER", sNummer),
                                New XElement("DATUM", sDatum),
                                New XElement("KUNDENNR", sKundenNr),
                                New XElement("KUNDENNAME", sKundenName),
                                New XElement("BUKREIS", sBukreis))))

Open in new window


Please help thanks, Greg
spen_langAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi spen_lang;

First we need to correct what you want as an XML document. The resulting document you posted in your question reposted below in a code block
<?xml version="1.0" encoding="utf-8"?>
<ns0:IMG_DOCUMENT xmlns:ns0="urn://nagelgroup.com">
<DocumentProcessorGroups>
    <group>
      <PageCount>1</PageCount>
      <FileCount>1</FileCount>
      <FileName>C:\12345</FileName>
      <Barcode>1223458421</Barcode>
    </group>
    <Variables>
      <NUMMER></NUMMER>
      <DATUM></DATUM>
      <KUNDENNR></KUNDENNR>
      <KUNDENNAME></KUNDENNAME>
      <BUKREIS></BUKREIS>
    </Variables>
</DocumentProcessorGroups>

Open in new window

Is not a Well Formed Document and therefore incorrect. The position you inserted the second line in above code block is invalid as well as it is missing the end tag. An XML document can only have one root node and in the above XML that is <DocumentProcessorGroups> and all other nodes in the document must be a child of that node. The below XML is Well-Formed because it is a child of the root and the closing tag has been inserted.
<?xml version="1.0" encoding="utf-8"?>
<DocumentProcessorGroups>
    <ns0:IMG_DOCUMENT xmlns:ns0="urn://nagelgroup.com"></ns0:IMG_DOCUMENT>
    <group>
      <PageCount>1</PageCount>
      <FileCount>1</FileCount>
      <FileName>C:\12345</FileName>
      <Barcode>1223458421</Barcode>
    </group>
    <Variables>
      <NUMMER></NUMMER>
      <DATUM></DATUM>
      <KUNDENNR></KUNDENNR>
      <KUNDENNAME></KUNDENNAME>
      <BUKREIS></BUKREIS>
    </Variables>
</DocumentProcessorGroups>

Open in new window

Please verify what the XML output should look like.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.