这就是我的代码:
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; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。