2012-08-14 5 views
2

다음 xml을 XmlDocument에 있습니다. 나는 그것에서 className를 추출하는 것을 시도하고있다.xml에서 속성을 검색하는 방법

<Registration className="Eng" type="DirectRegistration" state="Activated"  xmlns="http://xyz/Registration"> 
    <Fields> 
    <Field type="abc" value="123456" /> 
    <Field type="xyz" value="789" /> 
    </Fields> 
</Registration> 

다음 코드를 시도했지만 그 중 하나가 className이 아닙니다.

var xmlNode = xmlDoc.DocumentElement; 

누구나 className 값을 얻는 데 도움을 줄 수 있습니까?

많은 감사

+0

를 검색하는 XPath를 사용할 수 있습니까? 이것은 아마도 이미 많은 질문에서 논의 된 가장 일반적인 요청 중 하나 일 것입니다. – walther

답변

4

당신은 거의 있었다 :

var className = xmlDoc.DocumentElement.GetAttribute("className"); 

xmlDoc.DocumentElement

는 당신에게 전체 요소를 제공합니다; GetAttribute은 이름 지정된 개별 속성을 가져옵니다. 이 사용

+0

정말 좋은 답변입니다! –

1

시도 :

// Trying to parse the given file path to an XML 
XmlReader firstXML = XmlReader.Create(XMLPath); 
firstXML.ReadToFollowing("Registration"); 
firstXML.MoveToAttribute("className"); 
var res = firstXML.Value; 

res는 "클래스 이름"값을 개최한다.

1

당신은 또한 당신이 지금까지 시도 무엇 속성

string className = xmlDocument.SelectSingleNode("//Registration/@className").Value; 
관련 문제