James
Yes, building XDocuments using LINQ to XML in either VB.NET or C# in that
way is quite simple, but its much nicer (and easier) using XML literals in
VB.NET
(XML Literals are N/A as of C# 3.0 I am not sure about C# 4.0).
Its not a big deal to add the XML Declaration as just a bit of text, but it
just seemed to me that it would be logical to be able to do this:
Dim xd As New XDeclaration("1.0", "utf-8", "yes")
' no - can't embed a literal for the XDeclaration
ParkTestVideoXMLFile =
<%= xd %>
<TestResults>
<TestResult personid="0" code=<%= CurrentPatientCode %>
clinicianid="4" requestid="0" datetime=<%= CurrentTime1 %>>
<Test id=<%= CurrentTestName %> datetime=<%=
CurrentTime2 %>>
<Data type="Videofile" values=<%= sSaveFileName &
sVideoExt %>></Data>
</Test>
</TestResult>
</TestResults>
As my comments and the VS2010 decorations indicate, the problem is that my
XDeclaration xd cannot be used as an XML Literal.
_____
Ian Thomas
Victoria Park, Western Australia
From: [email protected] [mailto:[email protected]]
On Behalf Of James Chapman-Smith
Sent: Tuesday, November 20, 2012 9:48 AM
To: ozDotNet
Subject: RE: LINQ to XML question
Hi Ian,
I might have misunderstood what you're after, but doesn't `
ParkTestVideoXMLFile.Root.Add(/* content */)` work to solve this problem?
Cheers.
James.
From: [email protected] [mailto:[email protected]]
On Behalf Of Ian Thomas
Sent: Monday, 19 November 2012 11:39
To: [email protected]
Subject: LINQ to XML question
I have been using LINQ to XML in VB.NET (its more useful to me than C#
because of XML literals), but in a very naïve and simple way. Now, I want to
make my code more flexible.
Currently (ParkTestVideoXMLFile is of type XDocument) I do this -
ParkTestVideoXMLFile =
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TestResults>
<TestResult personid="0" code=<%= CurrentPatientCode %>
clinicianid="4" requestid="0" datetime=<%= CurrentPatient_StartTime %>>
<Test id=<%= CurrentTestName %> datetime=<%=
CurrentTest_StartTime %>></Test>
<Data type="Videofile" values=<%= sSaveFileName &
sVideoExt %>></Data>
</TestResult>
</TestResults>
which creates something like this
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<TestResults>
<TestResult personid="0" code="1235" clinicianid="4" requestid="0"
datetime="2012-11-09T19:10:39.5477585+08:00">
<Test id="ProSupLeft"
datetime="2012-11-09T19:10:48.1862526+08:00"></Test>
<Data type="Videofile"
values="VR_1235_ProSupLeft_2012-11-09_191045.WMV"></Data>
</TestResult>
<!-- I would like to add many additional TestResult data here -->
</TestResults>
After writing the file to disk (ParkTestVideoXMLFile.Save(CurrentXMLFile,
SaveOptions.None)) I would like to insert additional TestResult data at the
location highlighted, at some future time. Users may terminate the program
at any time, sometimes electing not to save the most recent test results.
What would be your suggestions for doing this? Or, should I just retain each
Tests variables in arrays and construct the whole XDocument at the time of
exit?
I guess the simple question is: how do I insert? Do I have to traverse the
XML tree, and is this more complicated than simply maintaining a few arrays?
The memory load for this data is tiny.
_____
Ian Thomas
Victoria Park, Western Australia