none
Error writing empty element using XMLTextWriter

    Question

  • I am using Visual Basic 2015 to read an XML file and write it back out (verbatim) to a text string.

    I have set up an XMLReader to read the file, and an XMLTextWriter to generate the string.

    For each item it reads I use a case statement on the NodeType and use the appropriate command to write the information out to the text string.

    The problem I am having is when it comes across an empty string. At first I wasn't testing for an empty string. The program ran with no problems, but it left off an end tag for the empty element.

    I researched and came up with the following in the case statement for XmlNodeType.Element:

                                        If reader.IsEmptyElement = True Then
                                            writer.WriteFullEndElement()
                                        Else 
                                            writer.WriteStartElement(reader.Name)
                                        End If   

                                        (code continues to write the rest of the element and the rest of the file)

    However, whenever I try to run it, I get an error message:

    Processing Error.Token StartAttribute in state Element Content would result in an invalid XML document.

    I have also tried:

                                        writer.WriteStartElement(reader.Name)

                                        If reader.IsEmptyElement = True Then
                                            writer.WriteFullEndElement()
                                        End If  

                                        (code continues to write the rest of the element and the rest of the file)

    and I get the same error.  

    Can anyone help me?




    • Edited by GretchenF Wednesday, March 15, 2017 7:51 PM
    Wednesday, March 15, 2017 6:05 PM

All replies

  • Hello,

    Please post (using the code button) a small working example of the XML that you are working with so if someone is inclined to help they can see what you are working with.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, March 15, 2017 10:12 PM
    Moderator
  • <?xml version="1.0" encoding="utf-8"?>
    <ReturnBatch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <BatchHeader>
        <BatchID>020617xyz</BatchID>
        <BatchMode>PROD</BatchMode>
        <BatchDate>2016/09/06</BatchDate>
        <OrderCount>2</OrderCount>
        <UserFieldCount>1</UserFieldCount>
      </BatchHeader>
      <UserData>
        <UserID>ABC1234</UserID>
        <LocationID>NH1001</LocationID>
        <InstrumentID>123456</InstrumentID>
      </UserData>
      <BatchResult>
        <DateProcessed>2017-2-22</DateProcessed>
        <SuccessfulTestCount>1</SuccessfulTestCount>
        <ErrorTestCount>1</ErrorTestCount>
        <BatchErrorCount>0</BatchErrorCount>
        <Message>You have used 24 Of your 130 available licenses this month.</Message>
        <UsedTowardsLimit TestLimit="130" Period="Monthly">24</UsedTowardsLimit>
      </BatchResult>
      <PatientData>
        <OrderData>
          <PatientID>NH102002</PatientID>
          <OrderType>CALC</OrderType>
          <OptionalData>
            <UserField Title="Patient Name">Sue Smith</UserField>
          </OptionalData>
          <RequiredData>
            <RequiredTest Name="A1">11.27</RequiredTest>
            <RequiredTest Name="B2">42.21</RequiredTest>
            <RequiredTest Name="C3">206.2</RequiredTest>
            <RequiredTest Name="D4">288.82</RequiredTest>
            <RequiredTest Name="E5">4.76</RequiredTest>
          </RequiredData>
          <ResultData>
            <TestResult Name="N8" TestDate="2/23/2017" ResultValue="4.2">Low</TestResult>
            <OrderErrorCount>0</OrderErrorCount>
          </ResultData>
        </OrderData>
        <OrderData>
          <PatientID>NH1968EKM</PatientID>
          <OrderType>CALCULATION</OrderType>
          <OptionalData>
            <UserField Title="Patient Name">John Doe</UserField>
          </OptionalData>
          <RequiredData>
            <RequiredTest Name="A1">&gt;3000</RequiredTest>
            <RequiredTest Name="B2">5.2</RequiredTest>
            <RequiredTest Name="C3">4..3</RequiredTest>
            <RequiredTest Name="D4">3.4</RequiredTest>
            <RequiredTest Name="E5" />
          </RequiredData>
          <ResultData>
            <TestResult Name="N8" TestDate="2/21/2017" ResultValue="0">Error</TestResult>
            <OrderErrorCount>4</OrderErrorCount>
            <OrderErrorsList>
              <OrderError FieldName="Test Result For C3">No data or invalid data sent for required test result  - Test Result For C3</OrderError>
              <OrderError FieldName="Test Result For A1">No data or invalid data sent for required test result  - Test Result For A1</OrderError>
              <OrderError FieldName="Test Result For E5">No data or invalid data sent for required test result  - Test Result For E5</OrderError>
              <OrderError FieldName="OrderType">OrderType must be CALC or RESEND - CALCULATION</OrderError>
            </OrderErrorsList>
          </ResultData>
        </OrderData>
      </PatientData>
    </ReturnBatch>
    Thursday, March 16, 2017 1:51 PM