XmlTextReader

Been trying to read a simply xml file.

Looked at and tried a few samples with no luck.

Funny thing is, I do this all the time in actionscript.

But, here is the xml file....

< xml version="1.0" >

<Parms>

<Name>test1</Name>

<Uname>test2</Uname>

<PW>test3</PW>

<Server>smtp.isp.com</Server>

<Pop>smtp.isp.com</Pop>

<Email>email@isp.net</Email>

</Parms>

And here is a sample I have been trying to get to work.

XmlTextReader myReader = null;

myReader = new XmlTextReader (xmlFile);// xmlFile is path to file

while (myReader.Read())

{

if (myReader.NodeType == XmlNodeType.Element)

{

string x;

x = myReader.Name.ToString(); // reads back Params

if (myReader.LocalName.Equals("Name"))

{

txtName.Text = myReader.ReadString();

AL.Add(myReader.ReadString()); // AL is an ArrayList but never gets inside this if statement

}

}}

Suggestions

Thanks,

Zath



Answer this question

XmlTextReader

  • publicgk

    My only guess is that there is something wrong with the xml file that you are reading. Your loop works fine with the test case that I created:

    StringBuilder xml = new System.Text.StringBuilder();
    xml.Append("< xml version=\"1.0\" >");
    xml.Append("<Parms>");
    xml.Append("<Name>test1</Name>");
    xml.Append("<Uname>test2</Uname>");
    xml.Append("<PW>test3</PW>");
    xml.Append("<Server>smtp.isp.com</Server>");
    xml.Append("<Pop>smtp.isp.com</Pop>");
    xml.Append("<Email>email@isp.net</Email>");
    xml.Append("</Parms>");
    System.IO.TextReader stringReader = new System.IO.StringReader(xml.ToString());

    System.Xml.XmlReader myReader = new System.Xml.XmlTextReader(stringReader);
    while (myReader.Read())
    {
    if (myReader.NodeType == System.Xml.XmlNodeType.Element)
    {
    String x;
    x = myReader.Name.ToString();
    if (myReader.LocalName.Equals("Name"))
    {
    // This line will write "test1" to the console
    Console.WriteLine(myReader.ReadString());
    }

    }
    }


  • RX7

    Looks similar to what I have been trying, but get this compile error for the keyword using.

    Cannot implicitly convert type 'System.Xml.XmlTextReader' to 'System.IDisposable'

    And I'm using VS.Net 2003

    Zath


  • Jeff Wilcox - MSFT

    Ah, yea you will need to remove the Using if using Visual Studio 2003, XmlTextReader only implements IDisposable in .NET 2.0.

  • Mike Hanson

    Yes, it did work fine. I put in a test string and stepped through.

    Something I should have done in the beginning.

    The problem was and always has been this in the if statements...

    txtEmail.Text = myReader.ReadString();

    AL.Add(myReader.ReadString());

    I was calling the funtion from another form and the ArrayList elements were all coming back null.

    So, I added a string var and did this....

    sNodeRead = myReader.ReadString(); //sNodeRead is the new string var

    txtName.Text = sNodeRead;

    AL.Add(sNodeRead);

    It all came back fine.

    Zath


  • Jerry Boggess

    The following outputs 'test1':



    using (XmlTextReader reader = new XmlTextReader(xmlfile))
    {
    while (reader.Read())
    {
    if (reader.NodeType == XmlNodeType.Element)
    {
    if (reader.LocalName.Equals("Name"))
    {
    Console.WriteLine(reader.ReadString());
    }
    }
    }
    }



  • XmlTextReader