읽고

2012-11-07 2 views
0

내가 XML 파일이 있다고 가정하자 XML을 C#을에서 특정 속성을 인쇄하는 방법 :이 코드의 목적은 같은 LINQ를 사용하지 않고이 XML의 속성 캡션의 값을 읽을 필요가읽고

<?xml version="1.0" encoding="utf-8"?> 
<Test Description="Test XML" VersionFormat="123" ProtectedContentText="(Test test)"> 
    <Testapp> 
     <TestappA> 
      <A Id="0" Caption="Test 0" /> 
      <A Id="1" Caption="Test 1" /> 
      <A Id="2" Caption="Test 2" /> 
      <A Id="3" Caption="Test 3"> 
       <AA> 
        <B Id="4" Caption="Test 4" /> 
       </AA> 
      </A> 
     </TestappA> 
     <AA> 
      <Reason Id="5" Caption="Test 5" /> 
      <Reason Id="6" Caption="Test 6" /> 
      <Reason Id="7" Caption="Test 7" /> 
     </AA> 
    </Testapp> 
</Test> 

을 Unity3D에서 이것을 수행하십시오.이 코드를 작성하기 위해 밤새도록 보낸 후에 작동하지 않는 코드를 작성하십시오. 도와주세요!

코드 냈다 : 우리는 몇 가지 옵션이 특성을 잡아 내 XML 그런

XmlDocument x = new XmlDocument(); 
x.Load("Filename goes here"); 

와 우선 하중을 XmlDocument :

// XML settings 
XmlReaderSettings settings = new XmlReaderSettings(); 
settings.IgnoreWhitespace = true; 
settings.IgnoreComments = true;       

// Loop through the XML to get all text from the right attributes 
using (XmlReader reader = XmlReader.Create(sourceFilepathTb.Text, settings)) 
{ 
    while (reader.Read()) 
    { 
     if (reader.NodeType == XmlNodeType.Element) 
     { 
      if (reader.HasAttributes) 
      { 
       if (reader.GetAttribute("Caption") != null) 
       {         
        MessageBox.Show(reader.GetAttribute("Caption")); 
       }        
      } 
     } 
    } 
} 

답변

1

는 여기에 내가 XML을 처리하는 방법입니다.

XmlNodeList xnl = x.GetElementsByTagName("A"); 
foreach(XmlNode n in xnl) 
    MessageBox.Show(n.Attribute["Caption"].Value); 

을 각각의 요소는 당신이 태그에 대한 것을 반복 : 당신은 그냥 모든 캡션을 원하는 정말하지 않는 무엇을 걱정하는 경우, 당신은이 작업을 수행 할 수 있습니다.

더 나은 조언을 제공하려면 먼저 요구 사항에 대해 자세히 알아야합니다.

0

당신은 목록 우리는 Caption 속성 현재 문서의 모든 노드를 선택합니다 @ 표기법을 사용하는

XmlDocument doc = new XmlDocument(); 
doc.LoadXml(xmlstring); // Or use doc.Load(filename) to load from file 
XmlNodeList attributes = doc.DocumentElement.SelectNodes("//@Caption"); 
foreach (XmlAttribute attrib in attributes) 
{ 
    Messageox.Show(attrib.Value); 
} 

을 얻을 수 XPath를 사용할 수 있습니다. xpath에 대한 자세한 정보 - http://www.w3schools.com/xpath/xpath_syntax.asp