Resolved Wanting to add to XDocument to make more than one "entry" on a single write...

Program4Food

Member
Joined
Dec 6, 2021
Messages
16
Programming Experience
5-10
Wanting to add to XDocument to make more than one "entry" on a single write...

I think I am missing something here, not sure I understand how to do this.

I have some snippit code I massaged and got working fine, but when I add line ??? I get:

Severity Code Description Project File Line Suppression State
Error BC30452 Operator '+' is not defined for types 'XDocument' and 'XDeclaration'. vb-forms-dotnet 3.5 - xml 1 F:\VisualStudio\2015\vb-forms-dotnet 3.5 - xml 1\vb-forms-dotnet 3.5 - xml 1\Form1.vb 308 Active


I would appreciate insight on how to add another element with one write using LINQ, I had a hard enough time finding code that works just to write one element.

Line 6 is the line I added and then got the posted error.:
      ' used in original code, not used here  Dim empNM As XNamespace = "urn:lst-emp:emp"

        Dim xDoc As New XDocument(New XDeclaration("1.0", "UTF-16", Nothing), New XElement("Employees", New XElement("Employee", New XComment("Only 3 elements for demo purposes"), New XElement("EmpId", "5"), New XElement("Name", "Kimmy"), New XElement("Sex", "Female"))))

        ' try to add another element
        xDoc = xDoc + (New XDeclaration("1.0", "UTF-16", Nothing), New XElement("Employees", New XElement("Employee", New XComment("Only 3 elements for demo purposes"), New XElement("EmpId", "5"), New XElement("Name", "Kimmy"), New XElement("Sex", "Female"))))

        Dim sw As New System.IO.StringWriter()
        Dim xWrite As XmlWriter = XmlWriter.Create(sw)
        xDoc.Save(xWrite)
        xWrite.Close()

        ' Save to Disk
        xDoc.Save("F:\VisualStudio\DevResources\LINQxml-1.xml")
        Console.WriteLine("Saved")
 
Last edited:
Okay, I had to work with it to understand it... you pointed me in the right direction!!

THANK YOU!


The following code...:
        Dim id As String = "99"
        Dim Name As String = "Balto"
        Dim sex As String = "Male"
        Dim commenttxt As String = "<-- This is a new comment -->"
        Dim filename As String = "F:\VisualStudio\DevResources\LINQxml-1.xml"
        Dim com As XComment = <!-- This is a custom comment -->

        Dim doc = XDocument.Load(filename)
        doc.Root.Add(<Employee>
                         <%= com %>
                         <EmpId><%= id %></EmpId>
                         <Name><%= Name %></Name>
                         <Sex><%= sex %></Sex>
                     </Employee>)
        doc.Save(filename)


... yielded the follow results:
  <Employee>
    <!-- This is a custom comment -->
    <EmpId>99</EmpId>
    <Name>Balto</Name>
    <Sex>Male</Sex>
  </Employee>
 
I appreciate you all's patience with me. I admit I am not very smart and maybe my brain is kinda slow
(between ADHD and PTSS).

Making code work and learning from it gives me a great sense of accomplishment.
It really does mean a lot to me! Thank you!
 
Dim com As XComment = <!-- This is a custom comment -->
That is not a "custom" comment as such, it just a standard xml literal with static text.
This is a comment where you can substitute the string with any string variable:
VB.NET:
Dim com As New XComment("This is a custom comment")
<%= com %>
Nice, didn't know you could do that.
 
Back
Top