使用XMLTextReader,我怎么知道我在哪个元素?

时间:2022-03-24 06:19:32

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

这是工作代码:(感谢大家的意见......如果没有你,就不可能做到!)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:

这就是我的XML的样子:

<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

我需要知道我是否正在阅读元素Creator Role =“Composer”或Creator Role =“Conductor”等

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

那么,使用XMLTextReader,我如何确定元素文本是什么?

2 个解决方案

#1


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;
        }
    }

#2


1  

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

你不能直到它真正阅读。 XmlTextReader按顺序读取流。

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

所以它恰恰相反:当你到达属性Role =“Composer”时,你可以知道你有什么元素。

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

考虑使用XPath,LINQ-To-XML或类似的:http://msdn.microsoft.com/en-us/library/bb156083.aspx

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

For XmlTextReader there is a XPathReader component somewhere:

对于XmlTextReader,在某处有一个XPathReader组件:

#1


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;
        }
    }

#2


1  

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

你不能直到它真正阅读。 XmlTextReader按顺序读取流。

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

所以它恰恰相反:当你到达属性Role =“Composer”时,你可以知道你有什么元素。

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

考虑使用XPath,LINQ-To-XML或类似的:http://msdn.microsoft.com/en-us/library/bb156083.aspx

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

For XmlTextReader there is a XPathReader component somewhere:

对于XmlTextReader,在某处有一个XPathReader组件: