VB NET XML

spen_lang
spen_lang used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Distinguished Expert 2017
Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial