2009-07-09 4 views
1

다음 xml 스트림을 읽으려고하고 있지만 정말 고민 중입니다. 요소가 시작 태그와 xml 스트림을 읽으십시오

<status>RUNNING</status> 

같은

그럼 I의 값이 확인 액세스 할 끝 태그 사이라면

<channelSnapshot xmlns="urn:betfair:games:api:v1"> 
<channel gameType="BLACKJACK" id="1444077" name="Exchange BlackJack"> 
<status>RUNNING</status> 
<game id="190675"> 
<round>1</round> 
<bettingWindowTime>30</bettingWindowTime> 
<bettingWindowPercentageComplete>100</bettingWindowPercentageComplete> 
<gameData> 
<object name="Player 1"> 
<description/> 
<status>IN_PLAY</status> 
<property name="Card 1" value="NOT AVAILABLE"/> 
<property name="Card 2" value="NOT AVAILABLE"/> 
</object> 

스트림은 다음과 같은 방식

Dim dataStream As Stream = response.GetResponseStream() 
    Dim reader As New XmlTextReader(dataStream) 

에서 획득된다. 내가 선택 사례 XMLNodeType과를 사용하지만 NODETYPE 다음 줄에

<property name="Card 1" value="NOT AVAILABLE"/> 

내가 이외에는 아무 것도 얻을 질수 나는 whitespace.So 넘어 요소에 얻을 수없는 공백 때이 사용하고있다 단어 속성.

명백한 것처럼 이것은 모두 새로운 것이므로 모든 도움을 환영 할 것입니다.

답변

1

다른 접근 방법은 어떻습니까? 현재 스트림을 처리하는 것은 상당히 어려운 작업 인 것 같습니다.

대신 전체 스트림을 문자열로 읽은 다음 해당 문자열을 XDocument에로드하면 훨씬 쉽게 파일을 처리 할 수 ​​있습니다.

' get the response stream so we can read it 
Dim responseStream = response.GetResponseStream() 
' create a stream reader to read the response 
Dim responseReader = New IO.StreamReader(responseStream) 
' read the response text (this should be javascript) 
Dim responseText = responseReader.ReadToEnd() 

' load the response into an XDocument 
Dim xmlDocument = XDocument.Parse(responseText) 

' find all the player objects from the document 
For Each playerObject In xmlDocument...<object> 

    ' display the player's name (this is how you access an attribute) 
    Console.WriteLine("Player name: {0}", [email protected]) 
    ' display the player's status (this is how you access an element) 
    Console.WriteLine("Player status: {0}", playerObject.<status>.Value) 

Next 

하면 다음과 같은 작업을 수행 할 수 플레이어 속성을 얻으려면 :

VB는 매우 쉬운 방법으로 XML 파일의 데이터에 액세스 무슨 뜻인지 확인하기 위해 다음 코드를 살펴 할 수 있습니다 : 사람으로

' go through the player's properties 
For Each playerProperty In playerObject...<property> 
    ' output the values 
    Console.WriteLine("Player property name: {0}", [email protected]) 
    Console.WriteLine("Player property value: {0}", [email protected]) 
Next 

다른 당신의 XML은 형식이 잘못되었습니다 언급,하지만하여 XDocument 그래서 당신은 그것을 고칠 수있을거야 이것에 대해 말할 것입니다.

+0

안녕하신 모든 분들께 감사드립니다! 모든 대답이 유용함이 증명되었고 나는 그들 모두로부터 무언가를 배웠습니다. 나는 아직도 내 프로젝트를 완료하는 데 필요한 모든 것을 알고 있는지 확신하지 못한다. 그러나 진전이 가장 확실하게 이루어졌다. 다시 한 번 감사드립니다. –

+0

스마일리, 프로젝트 도움이 행복 :-) –

0

속성으로 읽어야합니다. GetAttribute() 메소드를 참조하십시오. 예를 들어

:

Dim cardName as String = reader.GetAttribute("name") 
0

당신은 XmlReader를을 만들 때 기본이되는 스트림 (즉 XmlReaderSettings.IgnoreWhitespace를) 구문 분석을 단순화하기 위해 XmlReaderSettings을 사용하는 것이 좋습니다.

그런 다음 다음과 비슷한 방식으로 스트림을 구문 분석 할 수 있어야합니다.

using (XmlReader reader = XmlReader.Create(dataStream)) 
{ 
    while(reader.Read()) 
    { 
     switch(reader.NodeType) 
     { 
      case XmlNodeType.Element: 
      // do something 

      case XmlNodeType.Attribute: 
      // do something 

      // etc... 
     } 
    } 
} 

는 또한, 요소에서 가져 오는 방법을 결정하기 위해 propertiesXmlReader 기본 클래스의 methods를 검사, 특성, 및 기타 XML 엔티티.

0

XML이 올바른 형식이 아닙니다. 가까운 태그가없는 열린 태그가 있습니다. XML을 들여 썼다면 그걸 보았을 것입니다.

또한 .NET 1.1을 사용하는 경우를 제외하고는 XmlTextReader를 사용하지 않아야합니다. XmlReader.Create를 사용하십시오.

XmlReader를 직접 사용하는 것 외에도 XML 검색을위한 다소 간단한 모델 또는 XmlReader에서로드 할 수있는 이전 XmlDocument를 제공하는 LINQ to XML을 살펴볼 수 있습니다.

관련 문제