2011-11-20 114 views
4

这是我的代码如下所示:使用XMLTextReader,我怎么知道我在哪个元素上?

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; 

这是工作代码:(!感谢大家对你的输入...不能做它没有你)斯波坎 - 多德

   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> 

我需要知道,如果我读的元素造物主的角色=“作曲”创建者角色=“指挥者”

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

+0

你发布的XML是无效的。请张贴实际的XML,或至少一个重现问题的小例子。 –

+0

您使用的是什么版本的.NET? –

+0

.NET是3.5 ...我如何发布XML(大约50行...) – SpokaneDude

回答

1

这个怎么样?我希望它对你有用

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

你不能直到它实际阅读。 XmlTextReader依次读取一个流。

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

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

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

对于XmlTextReader的有一个XPathReader组件的地方:

+0

这里是我的代码... – SpokaneDude

+0

这是我的代码: 'case“Creator”: br.Read(); br.MoveToContent(); //给出角色的内容 tbComposer.Text = br.Value; br.Read(); br.MoveToContent(); //给出角色的内容 tbConductor.Text = br.Value; br.Read(); br.MoveToContent(); //给出角色' – SpokaneDude

+0

的内容你可以更新这个问题吗?阅读那条评论让我头疼:) – sehe

相关问题