WriteXML

Hello.  I have a dataset that I filled with data and I want to write it to an xml file.  I know how to use the dataset.writexml() command and it does not output the data exactly like I need.  The xml output contains the xml declaration information and the dataset name as the root node.  I do not need these.  I found out how to remove the xml declaration with the following code:

Dim xws As Xml.XmlWriterSettings = New Xml.XmlWriterSettings()
xws.ConformanceLevel = Xml.ConformanceLevel.Fragment
xws.Encoding = System.Text.Encoding.Default
xws.Indent =
True
xws.IndentChars = " "
xws.OmitXmlDeclaration = True

Dim xw As Xml.XmlWriter = Xml.XmlWriter.Create("c:\aa.xml", xws)
ds.WriteXml(xw)

Now I need to remove the dataset name from the XML output.  Example:

<dsName>
    <FirstTable>
         <ChildTable>
         </ChildTable>
    </FirstTable>
</dsName>

Needs to be:
<FirstTable>
    <ChildTable>
    </ChildTable>
</FirstTable>

Thanks for the help!  Sorry if this is a dup post, I could not find one though.
Aaron Moore




Answer this question

WriteXML

  • Richard Morgan

    Thanks for the help! I added what you said to it, and it worked, except now I have another problem.

    In the xml file one of the tags contains xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"- What is this and how do I remove it See <ThirdTable> .

    <FirstTable>
    <SecondTable>
    <
    ThirdTable ID="AM" xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

    <SecondTable>
    <FirstTable>

    My current code-

    Dim
    xws As Xml.XmlWriterSettings = New Xml.XmlWriterSettings()

    xws.ConformanceLevel = Xml.ConformanceLevel.Fragment

    xws.Encoding = System.Text.Encoding.Default

    xws.Indent = True

    xws.IndentChars = " "

    xws.OmitXmlDeclaration = True

    Dim xw As Xml.XmlWriter = Xml.XmlWriter.Create("c:\aa.xml")

    Dim dataDocument As New Xml.XmlDataDocument(ds)

    dataDocument.DocumentElement.WriteContentTo(xw)

    xw.Flush()

    xw.Close()



  • Jo&amp;#227;o H. Kiehn Jr.

    I was able to get it to work. My relationship in the dataset was setup wrong, and was causing a null value like you said. I got it fixed. I could have lived with the nil in the file, but the person receiving the file needed it removed.

    Thanks,
    Aaron



  • Andrei Faber

    The namespace http://www.w3.org/2001/XMLSchema-instance is defined as part of the W3C XML Schema specification. The attribute with name nil in that namespace can be used in XML instance documents to indicate that an element is empty even if it has some type, somehow comparable to a null value in a db column.

    If you can't live with such attributes in the XML then I guess my quick shot trying to solve your problem by feeding the dataset to an XmlDataDocument and write out the contents of the root element is not for you.



  • Colin Reid

    And what output would you want if the data set contains more than one table If there is no root element then the output would no longer be well-formed if each table results in a top level element.

    As for the problem one way might be to use e.g.

    Dim dataDocument as new XmlDataDocument(yourDataset)

    dataDocument.DocumentElement.WriteContentTo(xmlWriter)



  • Rob kreger

    Wouldnt the root be "<FirstTable>" All of the other tables are nested in the first. I just want to remove the dataset name tag from the xml output.

  • WriteXML