2015-02-06 105 views
0

我有以下XML結構:XMLReader不能讀取文件

<event id="0" start_time="2014-05-21 10:00:00" duration="86340"> 

    <short_event_descriptor lang="alb" name="Programet e Kanalit A1">Programe/Emisione</short_event_descriptor> 

    <extended_event_descriptor lang="alb"> 

    <text>Programet dhe Emisionet e perditshme te kanalit A1</text> 

    </extended_event_descriptor> 

</event> 

<event id="1" start_time="2014-05-22 10:00:00" duration="86340"> 

    <short_event_descriptor lang="alb" name="Programs of Channel A1">Programs/TalkShows</short_event_descriptor> 

    <extended_event_descriptor lang="alb"> 

    <text /> 

    </extended_event_descriptor> 

</event> 

和代碼我正在嘗試以下內容:

try 
      { 

       while (reader.Read()) 
       { 

         switch (reader.NodeType) 
         { 
          case XmlNodeType.Element: // The node is an element. 
           if (reader.Name == "channel") 
           { 
            while (reader.MoveToNextAttribute()) 
            { 
             if (reader.Name == "name") 
             { 
              // epg = new epg2(); 
              epg.channelname = reader.Value; 
              epg.number = Convert.ToInt32(channelNames[i][1]); 
              xmlfile.Add(epg); 
             } 
            } 
           } 
           if (reader.Name == "event") 
           { 
            while (reader.MoveToNextAttribute()) 
            { 
             if (reader.Name == "start_time") 
             { 
              // Ev = new Event(); 
              Ev.starttime = Convert.ToDateTime(reader.Value); 
             } 
             else if (reader.Name == "duration") 
             { 
              Ev.duration = Convert.ToInt32(reader.Value); 
             } 
             else if (reader.Name == "name") 
             { 
              Ev.duration = Convert.ToInt32(reader.Value); 
             } 
            } 
           } 
           if (reader.Name == "short_event_descriptor") 
           { 
            while (reader.MoveToNextAttribute()) 
            { 
             if (reader.Name == "name") 
             { 
              Ev.name = reader.Value; 
             } 
            } 
           } 

           break; 
          case XmlNodeType.Text: //Display the text in each element. 
           if (shenjuar > 0) 
           { 
            try 
            { 
             Ev.shortDescription = reader.Value.Replace("\r\n", "").Replace("\r", "").Replace("\n", "").Replace("Sub. Ang", "").Replace("Sub. Alb", "").Replace('\"', '\''); ; 
            } 
            catch 
            { 
             Ev.shortDescription = reader.Value; 
            } 
            shenjuar *= -1; 
           } 
           else if (shenjuar < 0) 
           { 
            try 
            { 
             Ev.longDescription = reader.Value.Replace("\r\n", "").Replace("\r", "").Replace("\n", "").Replace("Sub. Ang", "").Replace("Sub. Alb", "").Replace('\"', '\''); ; 
            } 
            catch 
            { 
             Ev.longDescription = reader.Value; 
            } 
            shenjuar *= -1; 
            epg.EventNumber.Add(Ev); 
           } 
           else reader.Skip(); 
           break; 
          case XmlNodeType.EndElement: //Display the end of the element 
           reader.Skip(); 
           break; 

         } 
        } 
        xmlfile.Add(epg); 
       } 

      catch (FileNotFoundException fnfe) 
      { 
       TextBox1.Text = "Error. Can not find " + CName + ".xml file"; 
       //return; 
      } 
      catch (Exception exc) 
      { 
       TextBox1.Text = "Unknown Error: \r\nError on " + CName + ".xml file \r\n Error Description:" + exc.Message; 
       //return; 
      } 
     } 

的一點是,這段代碼被用於XML文件的列表,而問題是,一些文件的標籤是自封閉,在這種情況下,讀者從閱讀跳過他們所有。由於XML文件作爲標準被引入公司,並且每天都會更改其內容,所以我無法更改它們的結構。我試圖使用IsEmptyElement屬性,我嘗試了不同的方式來讀取數據,但不管怎樣,它總是跳過帶有空標記的文件。現在一個星期我將嘗試每一個教程中,我能找到的了,可是不應對已工作

回答

0

你的代碼,跳過你的text元素:

if (reader.IsStartElement("text")) 
    reader.Skip(); 

刪除,並檢查自動關門元素XmlReader.IsEmptyElement

+0

你看到的那段代碼就是「最後的版本」。在它之前,我已經試過沒有reader.Skip屬性,並且如果它是空的,我檢查了文本元素,但是這也不起作用 – Ange1 2015-02-06 16:18:49