Visual Basic.NET - Crear Xml con XmlTextWriter

 
Vista:

Crear Xml con XmlTextWriter

Publicado por Elena P (1 intervención) el 19/08/2011 10:48:51
Hola,
Estoy trabajando con Visual Studio 2010, .NET Framework 4.

No consigo generar el xml correctamente...
Con el WriteStarElement no consigo generar el elemento con prefijo y sin namespace URI al elemento...

Xml a conseguir:
<cac:PrepaidPayment>
<cbc:ID>normalizedString</cbc:ID>
<cbc:PaidAmount currencyID="AED">1.0 currencyID="AED"</cbc:PaidAmount>
</cac:PrepaidPayment>


Public Sub PRU()
Dim xmlw As System.Xml.XmlTextWriter
Dim Valor As Double = 1.0
Dim ValorMoneda As String = "AED"

'crear el objeto de escritura xml
xmlw = New System.Xml.XmlTextWriter(filename, Nothing)

'Use automatic indentation for readability.
xmlw.Formatting = System.Xml.Formatting.Indented

xmlw.WriteStartElement("cac", "PrepaidPayment", "xx")
'xmlw.WriteStartElement("cac", "PrepaidPayment", Nothing)

xmlw.WriteElementString("cbc", "ID", "xx", Valor.ToString)
xmlw.WriteElementString("cbc", "PaidAmount", "xx", Valor.ToString)
'xmlw.WriteAttributeString("currencyID", ValorMoneda)
'xmlw.WriteValue(Valor & " currencyID=""" & ValorMoneda & """")

xmlw.WriteEndElement() 'PrepaidPayment

' Write the XML to file and close the writer.
xmlw.Flush()
xmlw.Close()

'Read the file back in and parse to ensure well formed XML.
Dim doc As New XmlDocument()
'Preserve white space for readability.
doc.PreserveWhitespace = True
'Load the file.
doc.Load(filename)

'Write the XML content to the console.
Console.Write(doc.InnerXml)
End Sub

'Cadena resultante:
' <cac:PrepaidPayment xmlns:cac="xx">
' <cbc:ID xmlns:cbc="xx">1</cbc:ID>
' <cbc:PaidAmount xmlns:cbc="xx">1</cbc:PaidAmount>
'</cac:PrepaidPayment>

¿Qué es lo que estoy haciendo mal?? ¿Como puedo crear el Xml que quiero conseguir??

Gracias y un saludo
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder