1

I am trying to read attributes from an XML stream.

Here is an example of the XML code

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<catalog version="1.1">
<dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX4" format="XXX" starttime="XXX" endtime="XXX" extentleft="XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" />
<dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX4" format="XXX" starttime="XXX" endtime="XXX" extentleft="XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" />
<dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX4" format="XXX" starttime="XXX" endtime="XXX" extentleft="XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" />
</catalog>
</string>

The code I am using to read is as follows:

XmlTextReader readerXML = new XmlTextReader(responseStream);

while (readerXML.Read())
{
if (readerXML.HasAttributes)
{
    Console.WriteLine(readerXML.Name + " Attribute");
    for (int i = 0; i < readerXML.AttributeCount; i++)
    {
        readerXML.MoveToAttribute(i);
        Console.WriteLine("Name: " + readerXML.Name + ", Value: " + readerXML.Value);
    }
}
readerXML.MoveToElement();
}

However, it only prints out

string Attribute
Name: xmlns, Value: http://schemas.microsoft.com/2003/10/Serialization/

Any idea why the first element is considered the only "element" in the XML.

Thanks!

EDIT When i covert the stream to a string using the following code:

            WebResponse response = restWebRequest.GetResponse();
            Stream responseStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(responseStream);
            string responseFromServer = reader.ReadToEnd();
            textBox1.Text = responseFromServer;

I receive this text:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
&lt;catalog version="1.1"&gt;
&lt;dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX" format="XXX" starttime="XXX" endtime="XXX" extentleft="-XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" /&gt;
&lt;dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX" format="XXX" starttime="XXX" endtime="XXX" extentleft="-XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" /&gt;
&lt;dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX" format="XXX" starttime="XXX" endtime="XXX" extentleft="-XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" /&gt;
&lt;/catalog&gt;
</string>

Like the xml syntax is being converted to its entity?

Any idea why this is happening/how to fix it?

gberg927
  • 1,636
  • 9
  • 38
  • 51
  • 1
    FYI, you should not use `XmlTextReader` directly. You should use `XmlReader.Create()` instead. – John Saunders Sep 16 '11 at 16:32
  • It doesn't do that for me... it's working fine for me. Your exact code, just replacing the XmlTextReader creation with a line to load from a file. – Jon Skeet Sep 16 '11 at 16:36
  • Can't reproduce the issue. What is 'responseStream'? I'm using File.OpenText to mimic a Stream, but that code is working as it should be using the OpenText method. – StyxRiver Sep 16 '11 at 16:45
  • It is a stream received from a webrequest/webresponse. WebResponse = webrequest.GetResponse() Stream responseStream = response.getResponseStream() – gberg927 Sep 16 '11 at 16:57
  • It looks like you have your XML embedded in some element that's being treated like a string value. My guess is that something that wants to treat the content as a string value put all the escaped entities in there for you so it didn't get processed as XML. I don't think it was the conversion to a stream that did it, but a serialization process or something. – BlueMonkMN Sep 19 '11 at 14:37

1 Answers1

0

See I can never predict XMLReader behavior. Any tips on understanding?

I suggest avoiding Move methods, which will probably improve your reader's behavior.

Edit: However, I have tested your code and it prints all the attributes for me.

Community
  • 1
  • 1
BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146