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

c# – 使用XMLTextReader,我怎么知道我在哪个元素?,第1张

概述这就是我的代码: 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 ro 这就是我的代码:

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;

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

这就是我的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>

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

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

解决方法 这个样本怎么样?我希望它对你有用

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

以上是内存溢出为你收集整理的c# – 使用XMLTextReader,我怎么知道我在哪个元素?全部内容,希望文章能够帮你解决c# – 使用XMLTextReader,我怎么知道我在哪个元素?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1234112.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存