XML LINQ - output to XMLtextwriter - the headaches...

  • Thread starter Thread starter Rob W
  • Start date Start date
R

Rob W

Greetings,

I want to use XML LINQ to create xml data that can be transformed to html.
Due to the XML LINQ output being an anonymous type I'm founding it very very
difficult to work with.

I thought I could use an xmltextwriter and stringwriter and feed in the
results from the query (see code at bottom of post).

Firstly I've interchanged the stringWriter and xmltextwriter when I am
writing the xml output.
To use writeElementString to write the output from the query would involve
checking each element if it contained a value or writing more manual
elements in (<quote> start/end element), example xml is as follows:-

<quote>
<date>1st January 2011</date>
<author>Pete</author>
<category>Life</category>
<quotetext>Get out of bed to start the day</quotetext>
</quote>

It complains about the xml enddocument tag, can my requirement be done using
a string writer/xmltextwriter?
Any help and guidance would greatly be appreciated.


Dim doc As XDocument
doc = XDocument.Load("C:\temp\xmltest.xml")

Dim query = _

From quoteElement In doc.<quotefile>.<quote> _

Where quoteElement.<author>.Value = "Einstein" _

Select quoteElement

Dim sw = New StringWriter()

Dim settings = New XmlWriterSettings()

settings.Indent = True

Dim w = XmlTextWriter.Create(sw, settings)

w.WriteStartDocument()

For Each result In query

sw.Write(result)

Next

w.WriteEndDocument()

w.Close()

Console.WriteLine(sw.ToString())





Thanks
Rob
 
Greetings,

I want to use XML LINQ to create xml data that can be transformed to html.
Due to the XML LINQ output being an anonymous type I'm founding it very very
difficult to work with.

I thought I could use an xmltextwriter and stringwriter and feed in the
results from the query (see code at bottom of post).

Firstly I've interchanged the stringWriter and xmltextwriter when I am
writing the xml output.
To use writeElementString to write the output from the query would involve
checking each element if it contained a value or writing more manual
elements in (<quote> start/end element), example xml is as follows:-

<quote>
<date>1st January 2011</date>
<author>Pete</author>
<category>Life</category>
<quotetext>Get out of bed to start the day</quotetext>
</quote>

It complains about the xml enddocument tag, can my requirement be done using
a string writer/xmltextwriter?
Any help and guidance would greatly be appreciated.

Maybe I'm confused by what your trying to do... But, given this input file:

<?xml version="1.0" encoding="utf-8" ?>
<quotefile>
<quote>
<date>1st January 2011</date>
<author>Einstein</author>
<category>Life</category>
<quotetext>1Get out of bed to start the day</quotetext>
</quote>
<quote>
<date>1st January 2011</date>
<author>Pete</author>
<category>Life</category>
<quotetext>Get out of bed to start the day</quotetext>
</quote>
<quote>
<date>1st January 2011</date>
<author>Einstein</author>
<category>Life</category>
<quotetext>2Get out of bed to start the day</quotetext>
</quote>
<quote>
<date>1st January 2011</date>
<author>Pete</author>
<category>Life</category>
<quotetext>Get out of bed to start the day</quotetext>
</quote>
</quotefile>

and this code:

Option Strict On
Option Explicit On
Option Infer On

Module Module1

Sub Main()
Dim doc As XDocument = XDocument.Load("test.xml")
Dim quotes = From quoteElement In doc.Descendants("quote") Where quoteElement.Element("author").Value = "Einstein" Select quoteElement
Dim outDoc As New XDocument(New XElement("quotefile", quotes))
outDoc.Save("out.xml")
End Sub

End Module


I get this output:

<?xml version="1.0" encoding="utf-8"?>
<quotefile>
<quote>
<date>1st January 2011</date>
<author>Einstein</author>
<category>Life</category>
<quotetext>1Get out of bed to start the day</quotetext>
</quote>
<quote>
<date>1st January 2011</date>
<author>Einstein</author>
<category>Life</category>
<quotetext>2Get out of bed to start the day</quotetext>
</quote>
</quotefile>


Is this what your want?
 
That's exactly what I want.

Tomorrow I will read through the code (especially "Option Infer On" and "Dim
outDoc As New XDocument(New XElement("quotefile", quotes))") to ensure O
100% understand it.

Much simpler solution to what I was tyring achieve.
Happy :-)
 
Back
Top