Using XMLTextReader, how can I determine which element I'm working on?

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 to everyone for your contribution ... could not 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 Creative role = "Explorer" , etc.

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

+4
source share
2 answers

How about this sample? I hope you find this helpful.

  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; } } 
+1
source

You cannot until you read it. XmlTextReader reads the stream sequentially.

So it's more likely the other way around: you can find out what element you had when you reach the Role="Composer" attribute.

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

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

For XmlTextReader somewhere there is an XPathReader component:

+1
source

Source: https://habr.com/ru/post/1382194/


All Articles