locked
LINQ XML - Writing output issues with stringwriter and xmlwriter

    Question

  • Greetings,

    I am attempting to Query xml data using LINQ, then put the results into a stringwriter which uses an xmlwriter.

    The code is below:-

            Dim doc As XDocument
            doc = XDocument.Load("C:\temp\xmltest.xml")
    
            Dim query = _
            From quoteElement In doc.<quotefile>.<quote> _
            Where quoteElement.<author>.Value = "God" _
            Select quoteElement
    
            Dim sw = New StringWriter()
    
            Dim settings = New XmlWriterSettings()
            settings.Indent = True
    
            Dim w = XmlWriter.Create(sw, settings)
            w.WriteStartDocument()
    
            For Each result In query
                sw.Write(result)
            Next
    
            'w.WriteEndDocument()
            w.Close()
    
            Console.WriteLine(sw.ToString())

    It errors with "Token EndDocument in state Document would result in an invalid XML document."

    Now oddly if I take the writeenddocument out the results is written first and then the writestartdocument xml.

    How can I create an xml document using the LINQ XML, stringwriter and xmlwriter?
    I was wondering if it is actually possible.

    I also tried this approach too with similar negative results
    For Each result In query
                w.WriteElementString(result.Name.ToString, result.Value.ToString)
     Next

    Thanks
    Rob
    • Edited by Robtyketto Friday, October 23, 2009 2:21 PM Add new code
    Friday, October 23, 2009 2:15 PM

Answers

  • Here was my solution:-

     'Validation over use XML LINQ query to find data and populate datagridview
            'if no data is found then inform user via messagebox
            Dim inputquotedoc As XDocument
            inputquotedoc = XDocument.Load(cQuoteFileName)
    
            Dim quotes = From quoteElement In inputquotedoc.Descendants("quote") _
            Where (quoteElement.Element("category").Value = cboCategory.SelectedItem.ToString) _
            And (quoteElement.Element("author").Value = txtAuthor.Text) _
            And (quoteElement.Element("text").Value Like "*" & txtKeyword.Text & "*") _
            And (quoteElement.Element("date").Value = dtpQuoteDate.Value.ToString("dd MMMM yyyy")) _
            Select quoteElement
    
            MessageBox.Show("Category value is " & cboCategory.SelectedItem.ToString)
            MessageBox.Show("Date value is " & dtpQuoteDate.Value.ToString("dd MMMM yyyy"))
    
            If quotes.Count > 0 Then
    
                Dim outQuoteDoc As New XDocument(New XElement("quotefile", quotes))
                Dim searchresults As String = Path.GetDirectoryName(cQuoteFileName) + "\quoteSearchResult.xml"
                outQuoteDoc.Save(searchresults)
    
                Dim quoteSearchReader As XmlTextReader = New XmlTextReader(searchresults)
                Dim xpathDoc As XPathDocument = New XPathDocument(quoteSearchReader)
                Dim xslpath As String = "C:\temp\xmltest.xsl"
    
                Dim xmlwritesettings As New XmlWriterSettings
                Dim htmlQuoteOuput As String = Path.GetDirectoryName(cQuoteFileName) + "\outputQuoteResults.html"
    
                Using quoteWriter As XmlWriter = XmlWriter.Create(htmlQuoteOuput, xmlwritesettings)
    
                    Dim transform As XslCompiledTransform = New XslCompiledTransform()
    
                    Try
                        transform.Load(xslpath)
                        transform.Transform(xpathDoc, Nothing, quoteWriter)
    
                    Catch ex As XsltException
    
                        'On Exception display error message
                        MessageBox.Show("Error." & vbCr & "Xml transformation error: " & ex.Message)
                        Exit Sub
    
                    End Try
    
                    If Not wbQuotes.Visible Then wbQuotes.Visible = True
                    wbQuotes.Navigate(htmlQuoteOuput)
    
                End Using
    
                cmdPrintQuote.Enabled = True
    
            Else
    
                MessageBox.Show("No rows found")
                Exit Sub
            End If
    
    • Marked as answer by Robtyketto Monday, October 26, 2009 11:21 AM
    Monday, October 26, 2009 11:20 AM

All replies

  • You can consider posting it at related LINQ forums for quicker and better support. Thanks!

    LINQ Project General Forum

    ADO.NET Entity Framework and LINQ to Entities Forum


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, October 26, 2009 10:11 AM
  • Here was my solution:-

     'Validation over use XML LINQ query to find data and populate datagridview
            'if no data is found then inform user via messagebox
            Dim inputquotedoc As XDocument
            inputquotedoc = XDocument.Load(cQuoteFileName)
    
            Dim quotes = From quoteElement In inputquotedoc.Descendants("quote") _
            Where (quoteElement.Element("category").Value = cboCategory.SelectedItem.ToString) _
            And (quoteElement.Element("author").Value = txtAuthor.Text) _
            And (quoteElement.Element("text").Value Like "*" & txtKeyword.Text & "*") _
            And (quoteElement.Element("date").Value = dtpQuoteDate.Value.ToString("dd MMMM yyyy")) _
            Select quoteElement
    
            MessageBox.Show("Category value is " & cboCategory.SelectedItem.ToString)
            MessageBox.Show("Date value is " & dtpQuoteDate.Value.ToString("dd MMMM yyyy"))
    
            If quotes.Count > 0 Then
    
                Dim outQuoteDoc As New XDocument(New XElement("quotefile", quotes))
                Dim searchresults As String = Path.GetDirectoryName(cQuoteFileName) + "\quoteSearchResult.xml"
                outQuoteDoc.Save(searchresults)
    
                Dim quoteSearchReader As XmlTextReader = New XmlTextReader(searchresults)
                Dim xpathDoc As XPathDocument = New XPathDocument(quoteSearchReader)
                Dim xslpath As String = "C:\temp\xmltest.xsl"
    
                Dim xmlwritesettings As New XmlWriterSettings
                Dim htmlQuoteOuput As String = Path.GetDirectoryName(cQuoteFileName) + "\outputQuoteResults.html"
    
                Using quoteWriter As XmlWriter = XmlWriter.Create(htmlQuoteOuput, xmlwritesettings)
    
                    Dim transform As XslCompiledTransform = New XslCompiledTransform()
    
                    Try
                        transform.Load(xslpath)
                        transform.Transform(xpathDoc, Nothing, quoteWriter)
    
                    Catch ex As XsltException
    
                        'On Exception display error message
                        MessageBox.Show("Error." & vbCr & "Xml transformation error: " & ex.Message)
                        Exit Sub
    
                    End Try
    
                    If Not wbQuotes.Visible Then wbQuotes.Visible = True
                    wbQuotes.Navigate(htmlQuoteOuput)
    
                End Using
    
                cmdPrintQuote.Enabled = True
    
            Else
    
                MessageBox.Show("No rows found")
                Exit Sub
            End If
    
    • Marked as answer by Robtyketto Monday, October 26, 2009 11:21 AM
    Monday, October 26, 2009 11:20 AM