Results 1 to 12 of 12

Thread: XmlTextWriter class question

  1. #1

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    XmlTextWriter class question

    Using the XmlTextWriter class, how would I write out the line bolded below with the results shown below:
    Code:
    <?xml version="1.0" ?>
    <?qbxml version="2.0"?>
    <QBXML>
      <QBXMLMsgsRq onError = "stopOnError">
        <CustomerAddRq requestID = "1">
          <CustomerAdd>
            <Name>Some Guy</Name>
          </CustomerAdd>
        </CustomerAddRq>
      </QBXMLMsgsRq>
    </QBXML>
    I am trying to start an element but also include addt'l information...
    namely, onError = "stopOnError"
    If I use
    VB Code:
    1. myXml.WriteStartElement("QBXMLMsgsRq onError='stopOnError'", Nothing)

    I get thiswhere the ending tag also includes the extra info)
    Code:
    <?xml version='1.0' ?>
    <QBXML>
      <QBXMLMsgsRq onError='stopOnError'>
        <CustomerAddRq requestID='1'>
          <CustomerAdd>
            <Name>Karl</Name>
          </CustomerAdd>
        </CustomerAddRq requestID='1'>
      </QBXMLMsgsRq onError='stopOnError'>
    </QBXML>ML>

    Now obviously I know why it makes the end tag the same...but I can't figure out how to get around that...
    I want the bolded line above to just read </QBXMLMsgsRq>. In case you are wondering, this is xml structured request for adding a customer to Intuit's Quickbooks.
    Last edited by nemaroller; Apr 23rd, 2003 at 12:08 PM.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Try WriteStartAttribute for adding the attribute.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    thanks Cander.... writes as expected now...

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You should check out the XMLDocument class, if you haven't already, with somethings it can be much easier to use.

  5. #5

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    While I got your attention:
    VB Code:
    1. myXml.WriteRaw("<?qbxml version = '2.0' ?>")

    How do I write the 2.0 inside a pair of double quotes?

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    try

    myXml.WriteRaw("<?qbxml version = ""2.0"" ?>")

    assuming it still does it like VB6 does.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    yea... well it works .WriteRaw, (loses the CRLF though),
    but for

    myXml.WriteStartAttribute("onError = ""stopOnError"", Nothing)

    it fails to even compile....

  8. #8

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I'm able to use the double quotes using this:
    Code:
    myXml.WriteStartAttribute("onError= " & c & "stopOnError" & c, Nothing)
    where c is defined a char type of value 34 (double quotes)


    But now have a problem related to my earlier question, see output below.

    Here's my code:
    Code:
    Dim myXml As XmlTextWriter = New XmlTextWriter(mySendStream, System.Text.Encoding.UTF8)
            Dim c As Char
            c = Chr(34)
    myXml.Formatting = System.Xml.Formatting.Indented
            myXml.WriteRaw("<?xml version = ""1.0"" ?>")
            myXml.WriteRaw("<?qbxml version = ""2.0"" ?>")
            myXml.WriteStartElement("QBXML", Nothing)
            myXml.WriteStartElement("QBXMLMsgsRq")
            myXml.WriteStartAttribute("onError= " & c & "stopOnError" & c, Nothing)
            myXml.WriteEndAttribute()
            myXml.WriteStartElement("CustomerAddRq")
            myXml.WriteStartAttribute("requestID = " & c & "1" & c, Nothing)
            myXml.WriteEndAttribute()
            myXml.WriteStartElement("CustomerAdd")
            myXml.WriteElementString("Name", Nothing, "Karl")
    
            'close tags
            myXml.WriteEndElement()
            myXml.WriteEndElement()
            myXml.WriteEndElement()
            myXml.WriteEndElement()
            myXml.Flush()
            myXml.Close()

    Here's the output (problems in bold):
    VB Code:
    1. <?xml version = "1.0" ?><?qbxml version = "2.0" ?>
    2. <QBXML>
    3.   <QBXMLMsgsRq onError= "stopOnError"[color=red]=""[/color]> 'the writeStartAttribute problem
    4.     <CustomerAddRq requestID = "1"[color=red]=""[/color]>
    5.       <CustomerAdd>
    6.         <Name>Karl</Name>
    7.       </CustomerAdd>
    8.     </CustomerAddRq>
    9.   </QBXMLMsgsRq>
    10. </QBXML>
    Last edited by nemaroller; Apr 23rd, 2003 at 12:48 PM.

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Its because you didn't give it a value properly, try using the WriteAttributeString method instead:
    VB Code:
    1. myXml.WriteRaw("<?xml version = ""1.0"" ?>")
    2.         myXml.WriteRaw("<?qbxml version = ""2.0"" ?>")
    3.         myXml.WriteStartElement("QBXML", Nothing)
    4.         myXml.WriteStartElement("QBXMLMsgsRq")
    5.         myXml.WriteAttributeString("onError", "stopOnError")
    6.         myXml.WriteStartElement("CustomerAddRq")
    7.         myXml.WriteAttributeString("requestID", "1")
    8.         myXml.WriteStartElement("CustomerAdd")
    9.         myXml.WriteElementString("Name", Nothing, "Karl")

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can also use WriteProcessingInstruction to write the top part out and have it on two lines.

    VB Code:
    1. myXml.WriteProcessingInstruction("xml", "version=""1.0""")
    2.         myXml.WriteProcessingInstruction("qbxml", "version=""2.0""")

  11. #11

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Edneeis.... thank you very much for your response... it 's all kosher now...

  12. #12

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Funny thing is... the only way I got these XML files to work was to change the encoding from UTF8 to ASCII. I suppose that was a limitation of Intuit's Quickbooks XML parser ...

    VB Code:
    1. Dim mySendStream As IO.FileStream = New FileStream("C:\mxml1.xml", FileMode.Create)
    2.         Dim myXml As XmlTextWriter = New XmlTextWriter(mySendStream, [b]System.Text.Encoding.ASCII[/b])
    3.         myXml.Formatting = System.Xml.Formatting.Indented
    4.  
    5.         'write xml version and encoding
    6.         myXml.WriteStartDocument()
    7.         'write QuickBook's QBXML version
    8.         myXml.WriteProcessingInstruction("qbxml", "version=""1.1""")
    9.  
    10.         'write data
    11.         myXml.WriteStartElement("QBXML", Nothing)
    12.         myXml.WriteStartElement("QBXMLMsgsRq")
    13.         myXml.WriteAttributeString("onError", "stopOnError")
    14.         myXml.WriteStartElement("CustomerAddRq")
    15.         myXml.WriteAttributeString("requestID", "1")
    16.         myXml.WriteStartElement("CustomerAdd")
    17.         myXml.WriteElementString("Name", Nothing, "Dirk Diggler")
    18.  
    19.         'close tags
    20.         myXml.WriteEndDocument()
    21.         myXml.Flush()
    22.         myXml.Close()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width