简体   繁体   中英

VB.NET having trouble writing a string into a root node

        Dim doc As New XmlDocument()
        Dim nodes As XmlNodeList
        doc.Load("test.xml")
        nodes = doc.SelectNodes("/Subjects/" & comboSubject.Text)

        Dim fileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.xml")

        Dim Rows As List(Of DataGridViewRow) =
            gridTests.Rows.Cast(Of DataGridViewRow).Where(Function(row) Not row.IsNewRow).ToList

        Dim xmlData As String =
        <<%= comboSubject.Text %>>
            <%= From row In Rows
                Select
                    <attempt>
                        <test><%= CStr(row.Cells("Test").Value) %></test>
                        <score><%= CStr(row.Cells("Score").Value) %></score>
                    </attempt> %>
        </>.ToString


        For Each node As XmlNode In nodes
            If node IsNot Nothing Then
                node.ParentNode.RemoveChild(node)
                doc.Save("test.xml")
            End If
        Next

How do I write the xmlData string into the Subjects node?

<?xml version="1.0" encoding="UTF-8"?>
<Subjects>
  <History>
    <attempt>
      <test>1999</test>
      <score>75</score>
    </attempt>
    <attempt>
      <test>1987</test>
      <score>50</score>
    </attempt>
    <attempt>
      <test>1789</test>
      <score>25</score>
    </attempt>
  </History>
</Subjects>

Whenever I try to write the XmlData string into test.xml it ends up either deleting everything or doing nothing at all. I'm not sure what I'm doing wrong here. Any solution to this would be very much appreciated.

I see a few issues...

Dim xmlData As String =
        <<%= comboSubject.Text %>>
            <%= From row In Rows
                Select
                    <attempt>
                        <test><%= CStr(row.Cells("Test").Value) %></test>
                        <score><%= CStr(row.Cells("Score").Value) %></score>
                    </attempt> %>
        </>.ToString

This doesn't appear to be producing valid XML as the tag is not closed properly. It should be, for example, </History> rather than </>

Further, you aren't doing anything with this XML other than storing it in the xmlData variable. The variable is never used. You only go on to delete the existing node, which is why you see it "delete everything"

For Each node As XmlNode In nodes
    If node IsNot Nothing Then
        node.ParentNode.RemoveChild(node)
        doc.Save("test.xml")
    End If
Next

doc represents your entire document and node would be, for instance, /Subjects/History so when you say node.ParentNode.RemoveChild(node) you are removing the History node from its parent Subjects node leaving you with only <Subjects/> being saved. This loop would also call save for every iteration which isn't necessary, but that is neither here nor there.

Finally, as mentioned, you are not writing the xmlData back to the document anywhere. There isn't a great answer for how to do that, because XML isn't meant to be built as a string in this manner. You can use the XmlNode.InnerXml property to change the markup of an element directly but, as Microsoft notes in the document, it isn't a great idea. You could also parse that string into an XML object, but ideally you should just be constructing Node objects for each of the elements you are creating and then adding them to doc in the appropriate place. Something like this, as an example:

Dim objHistory as XmlElement = doc.CreateElement("History")
Dim objAttempt As XmlElement = doc.CreateElement("attempt")
Dim objTest As XmlElement = doc.CreateElement("test")
Dim objScore as XmlElement = doc.CreateElement("score")
objTest.InnerText = "1999"
objScore.InnerText = "75"
objAttempt.AppendChild(objTest)
objAttempt.AppendChild(objScore)
objHistory.AppendChild(objAttempt)
doc.FirstChild.AppendChild(objHistory)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM