2012-02-21 4 views
2

내 XML 섹션은 다음과 같이이다 :이 XML을 읽을 XMLReader를 사용하는 방법

<Note> 
<SpecialText att1="" /> 
</Note> 

또는

<Note> 
This is a note. 
</Note> 

내가 필요로하는 것은 XML을 읽는 XmlReader를 사용하는 것입니다,하지만 난 확실하지 않다

innerXml이 다른 xmlelement인지 아니면 텍스트인지 판단하는 방법.

while (reader.Read()) 
{ 
    if (reader.NodeType == XmlNodeType.Element) 
    { 
     switch (reader.LocalName.ToLower()) 
     { 
      case MMLElement.SpecialText: 
      //// read related attributes 
      break; 
     } 
    } 
} 

을하지만 주 아래에있는 것은 단지 텍스트의 경우 어떻게 내가 내용을 읽을 수 있습니다

나는이 일을하고있다. reader.ReadInnerXml을 사용하면 모든 것을 읽을 수 있으므로 SpecialText XmlElement인지 아니면 텍스트인지 알 수있는 기회가 없습니까?

많은 감사

답변

0

당신이 XElement.Load(file)를 사용하는 경우 다음 사용할 수 있습니다 ...

XElement xfile = XElement.Load(file); 
XElement note = xfile.Path("path/to/note"); 
if(note.HasElements) 
    // read the element 
else 
    string text = (string)note; 

참고 : 여기에 경로() 가져 오기 : https://github.com/ChuckSavage/XmlLib/blob/master/XElementExtensions.cs

0

지금이 생색 들릴지을하지만 이것이 레퍼런스를 파헤 치면 쉽게 대답 할 수 있다고 생각합니다. 그렇다면 다시 나는 당신의 문제를 완전히 이해하지 못할 수도 있습니다. 다음 답이 찾고있는 것이 아니라면 더 자세한 정보를 게시하면 기꺼이 도와 드리겠습니다.

while (reader.Read())    
    {    
    if (reader.NodeType == XmlNodeType.Element)    
    {    
     switch (reader.LocalName.ToLower())    
     {    
      case MMLElement.SpecialText:    
      //// read related attributes    
      break;    
     }    
    }    

    else if (reader.NodeType == XmlNodeType.Text) 
    { 
     string thisIsjustText = reader.value; 
    } 
    //whatever comes next 
} 
: 콘텐츠가 당신이 원하는대로 함께 할 다음 확인하고, 단지 텍스트의 경우

을 확인하려면
관련 문제