i have the following function which brings a nodelist and i need to add it to the following xml
Public Function Salida(ByVal est As String, ByVal err As String, Optional node As Object = Nothing) As String
Dim m_xmld = New XmlDocument
Try
m_xmld.LoadXml("<tx><estado></estado><info></info></tx>")
m_xmld.SelectSingleNode("tx/estado").InnerText = est
m_xmld.SelectSingleNode("tx/info").InnerText = err
If Not node Is Nothing Then
m_xmld.AppendChild(node)
End If
Catch ex As Exception
m_xmld.InnerText = "Error"
End Try
Return m_xmld.InnerXml
End Function
but when I try to add it, I get the following error "{"Unable to convert an object of type 'System.Xml.XmlElementList' to type 'System.Xml.XmlNode'."}"
how to name the function and obtain the node:
mvarXmlSalida = Util.Salida("0", "", xmldoc.GetElementsByTagName("file"))
<Transport>
<file>
<name>test.txt</name>
<size>1,52 Kb</size>
<type>.txt</type>
<data>test data</data>
<option>ExactName</option>
</file>
<Transport>
CodePudding user response:
It's not quite clear what you're trying to append, but the following should be helpful:
Add the following Imports
Imports System.IO
Imports System.Xml.Linq
Public Function Salida(est As String, err As String, Optional node As XElement = Nothing) As String
Dim xmlStr As String = "<tx><estado></estado><info></info></tx>"
'parse XML
Dim root As XElement = XElement.Parse(xmlStr)
If node IsNot Nothing Then
root.Elements("estado").FirstOrDefault().AddAfterSelf(node) 'option 1
'root.Elements("info").FirstOrDefault().AddAfterSelf(node) 'option 2
'root.Elements("estado").FirstOrDefault().AddFirst(node) 'option 3
'root.Elements("info").FirstOrDefault().AddFirst(node) 'option 4
End If
'save XML to string
Using tr As StringWriter = New StringWriter
root.Save(tr)
Return tr.ToString
End Using
End Function
Usage 1:
Dim xmlInput As String = "
<Transport>
<file>
<name>test.txt</name>
<size>1,52 Kb</size>
<type>.txt</type>
<data>test data</data>
<option>ExactName</option>
</file>
</Transport>"
Dim nodeTransport As XElement = XElement.Parse(xmlInput)
'node: Transport
Dim result As String = Salida("0", "", nodeTransport)
Debug.WriteLine(result)
Usage 2:
Dim xmlInput As String = "
<Transport>
<file>
<name>test.txt</name>
<size>1,52 Kb</size>
<type>.txt</type>
<data>test data</data>
<option>ExactName</option>
</file>
</Transport>"
Dim nodeTransport As XElement = XElement.Parse(xmlInput)
Dim nodeFile As XElement = nodeTransport.Elements("file").FirstOrDefault()
'node: file
Dim result As String = Salida("0", "", nodeFile)
Debug.WriteLine(result)
Result (Usage 2):
<?xml version="1.0" encoding="utf-16"?>
<tx>
<estado></estado>
<file>
<name>test.txt</name>
<size>1,52 Kb</size>
<type>.txt</type>
<data>test data</data>
<option>ExactName</option>
</file>
<info></info>
</tx>
Resources:
CodePudding user response:
A guess,
First your function re-written.
Public Function Salida(ByVal est As String,
ByVal err As String,
Optional node As XElement = Nothing) As XElement
Dim m_xmld As XElement
m_xmld = <tx><estado><%= est %></estado><info><%= err %></info></tx>
If Not node Is Nothing Then
m_xmld.Add(node)
End If
Return m_xmld
End Function
Second, showing it in use.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xe As XElement
xe = <Transport>
<file>
<name>test.txt</name>
<size>1,52 Kb</size>
<type>.txt</type>
<data>test data</data>
<option>ExactName</option>
</file>
</Transport>
Dim result As XElement
result = Salida("EST", "ERR", xe.<file>.FirstOrDefault)
'result
'<tx>
' <estado>EST</estado>
' <info>ERR</info>
' <file>
' <name>test.txt</name>
' <size>1,52 Kb</size>
' <type>.txt</type>
' <data>test data</data>
' <option>ExactName</option>
' </file>
'</tx>
End Sub
As I mentioned in my comment, what you are trying to do is NOT CLEAR!