Capture actual xml node syntax using xmlTextReader

I'm a newbie to .net and am in the middle of a time crunch to get an application working. Each time I think I've got it something else won't work.

I'm receiving an xml stream and sending back another xml stream. I'm having trouble parsing the request stream. I can parse the individual nodes and recover the data correctly. My problem stems from the fact that I need to 'save' part of the stream in it's original form to add to my response xml stream. I'm able to store the nodes that I want using the following code:

If xvr.NodeType = XmlNodeType.Element Then
'Capture the PersonalData node in newXML, process later
newXML.Append(xvr.ReadOuterXml.ToString)

Because I've used the ReadOuterXML method I can not parse this section of the xml stream. Here's where I encounter my problem. I want to use this data as another xml stream and validate and parse it.

I've changed the newXML stringbuilder object to a byte stream using the following code:
Dim msXML As MemoryStream = New MemoryStream()
.
.
Dim b() As Byte
Dim utf As New System.Text.UTF7Encoding()
b = utf.GetBytes(newXML.ToString)

msXML.Write(b, 0, b.Length - 1)
msXML.Position = 0
Dim s As String = utf.GetString(b)

Console.WriteLine(s)

Since this is an xml node segment, it has no header record and throws the error:

System.XML.XMLException: There is invalid data at the root level. Line 1, Position 1 ..........

Now (finally) I get to my question. I'd like to capture the xmlDeclaration node of my request stream but I'm unable to do it. Right now I'm building the declaration node as follows:

If xvr.NodeType = XmlNodeType.XmlDeclaration Then
Console.WriteLine(xvr.Value)
newXML.Append("<?xml ")
newXML.Append(xvr.Value.ToString)
newXML.Append("?>")
'newXML.Append(Chr(13))
End If

However, I still get the same error and the first line output by console.WriteLIne (s) looks as follows:

<?xml version="1.0" encodeing="UTF-8"?><PersonalData>

When I try to use the append line feed option, it does really weird things.

So, my question is can I capture the actual syntax of the declaration node to append it to my stringbuilder object and, if so, how?

If this isn't possible, should I just re-read the supplied stream parsing only the parts that I need?

Thanks in advance,

Mary
 
Back
Top