4

This is what my code looks like:

case "Creator":
    br.Read();
    br.MoveToContent();  // gives the content of the role
    tbComposer.Text = br.Value;
    br.Read();
    br.MoveToContent();  // gives the content of the role
    tbConductor.Text = br.Value;
    br.Read();
    br.MoveToContent();  // gives the content of the role
    tbOrchestra.Text = br.Value;
    break;

This is the working code: (Thanks everybody for your input... couldn't have done it without you!) Spokane-Dude

                case "Creator":
                    br.MoveToFirstAttribute();
                    if (br.Value == "Composer") {
                        br.Read();
                        tbComposer.Text = br.Value;
                    }
                    if (br.Value == "Conductor") {
                        br.Read();
                        tbConductor.Text = br.Value;
                    }
                    if (br.Value == "Orchestra") {
                        br.Read();
                        tbOrchestra.Text = br.Value;
                    }
                    break;

This is what my XML looks like:

<ItemLookupResponse>
    <OperationRequest/>
    <Items>
        <Request/>
        <Item>
            <ItemAttributes>
                <Binding>Audio CD</Binding>
                <CatalogNumberList>
                    <CatalogNumberListElement>43850</CatalogNumberListElement>
                </CatalogNumberList>
                <Creator Role="Composer">Gioachino Rossini</Creator>
                <Creator Role="Conductor">Riccardo Chailly</Creator>
                <Creator Role="Orchestra">National Philharmonic Orchestra</Creator>
            </ItemAttributes>
        </Item>
    </Items>
</ItemLookupResponse>

I need to know if I am reading the element Creator Role="Composer" or Creator Role="Conductor", etc

So, using XMLTextReader, how can I determine what the element text is?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120

2 Answers2

1

You can't until its actually read. XmlTextReader reads a stream, sequentially.

So it is rather the other way around: you can know what element you had when you reach the attribute Role="Composer".

Consider using XPath, LINQ-To-XML or similar: http://msdn.microsoft.com/en-us/library/bb156083.aspx

node.XPathSelectElement(@"*/Creator[@Role=""Conductor""]");

For XmlTextReader there is a XPathReader component somewhere:

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Here's my code: ' case "Creator": br.Read(); br.MoveToContent(); // gives the content of the role tbComposer.Text = br.Value; br.Read(); br.MoveToContent(); // gives the content of the role tbConductor.Text = br.Value; br.Read(); br.MoveToContent(); // gives the content of the role' – SpokaneDude Nov 20 '11 at 23:48
  • Could you update the question instead? Reading that comment made my head hurt :) – sehe Nov 20 '11 at 23:49
  • Put that in your question, not in a comment – John Saunders Nov 20 '11 at 23:49
1

How about this sample ? I hope it would useful to you

   static void Main(string[] args)
    {

        string xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Creators><Creator Role=\"Composer\">Gioachino Rossini</Creator><Creator Role=\"Conductor\">Riccardo Chailly</Creator><Creator Role=\"Orchestra\">National Philharmonic Orchestra</Creator></Creators>";
        using (XmlReader xmlReader = XmlTextReader.Create(new StringReader(xmlStr)))
        {
            xmlReader.MoveToContent();
            xmlReader.ReadStartElement("Creators" , "");
            SomeMethod("Composer", xmlReader);
            SomeMethod("Conductor", xmlReader);
            SomeMethod("Orchestra", xmlReader);
        }

        Console.WriteLine("........");
        Console.Read();
    }

    static void SomeMethod(string role, XmlReader xmlReader)
    {
        xmlReader.MoveToAttribute("Role");

        switch (role)
        {
            case "Composer":
                {
                    xmlReader.MoveToContent();
                    Console.WriteLine(string.Format("Composer:{0}", xmlReader.ReadElementContentAsString()));

                } break;
            case "Conductor":
                {
                    xmlReader.MoveToContent();
                    Console.WriteLine(string.Format("Conductor:{0}", xmlReader.ReadElementContentAsString()));

                } break;
            case "Orchestra":
                {
                    xmlReader.MoveToContent();
                    Console.WriteLine(string.Format("Orchestra:{0}", xmlReader.ReadElementContentAsString()));

                } break;

            default: break;
        }
    }
shenhengbin
  • 4,236
  • 1
  • 24
  • 33