2011-11-20 94 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

相關問題