문제

<career code="17-1011.00">
   <code>17-1011.00</code>
   <title>Architects</title>
   <tags bright_outlook="false" green="true" apprenticeship="false" />
   <also_called>
      <title>Architect</title>
      <title>Project Architect</title>
      <title>Project Manager</title>
      <title>Architectural Project Manager</title>
   </also_called>
   <what_they_do>Plan and design structures, such as private residences, office buildings, theaters, factories, and other structural property.</what_they_do>
   <on_the_job>
      <task>Consult with clients to determine functional or spatial requirements of structures.</task>
      <task>Prepare scale drawings.</task>
      <task>Plan layout of project.</task>
   </on_the_job>
</career>
.

나는이 XML이 ONET에서 반환되었고, 사용하기 위해 정보를 구문 분석하고 싶습니다. 여기에 태그의 내부 텍스트를 시도하고 구문 분석하기 위해 작성한 코드는 '입력'이 ONET XML입니다.

 XmlDocument inputXML = new XmlDocument();
        inputXML.LoadXml(input);
        XmlElement root = inputXML.DocumentElement;
        XmlNodeList titleList = root.GetElementsByTagName("also_called");
        for (int i = 0; i < titleList.Count; i++)
        {
            Console.WriteLine(titleList[i].InnerText);
        } 
.

나는 크기 4의 노드리스트를 기대하고 있습니다.결과를 인쇄 할 때 결과는 1의 크기입니다. "ArchitectProject ArchitectProject ManagerArchitectural 프로젝트 관리자"

XMLNODELIST TITLELIST가 잘못되었습니다.XML 트리가 '_ _ _ _'아래의 '제목'태그의 내부 값을 얻기 위해 XML 트리를 어떻게 이동시키고 처리 할 수 있습니까?

도움이 되었습니까?

해결책

also_called라는 요소를 가져옵니다.귀하의 목록에는 하나의 요소가 하나뿐입니다.당신이 원하는 것은 also_called 노드의 아이들을 얻는 것입니다.

예 :

XmlNodeList also_calledList = root.GetElementsByTagName("also_called");
XmlNode also_calledElement = also_calledList[0];
XmlNodeList titleList = also_calledElement.ChildNodes;

foreach (XmlNode titleNode in titleList)
{
    Console.WriteLine(titleNode.InnerText);
}
.

XDocument 대신 XmlDocument 및 XML을 XML로 사용하는 것이 좋습니다. 훨씬 간단합니다.

XDocument root = XDocument.Parse(input);

foreach (XElement titleNode in root.Descendants("also_called").First().Elements())
{
    Console.WriteLine(titleNode.Value);
}
.

다른 팁

XPath의 비트 비트가 필요합니다.이것은 첫 번째 title의 자식 인 모든 also_called 노드를 선택합니다.

        XmlDocument inputXML = new XmlDocument();
        inputXML.LoadXml(input);

        foreach(var node in root.SelectNodes("also_called[1]/title"))
        {
            Console.WriteLine(node.InnerText);
        } 
.

GetElementsByTagName 또는 ChildNodes 및 ILK를 사용해야 할 필요가 없으며 원하는 사람인지 알아 내기 위해 노드를 검사해야합니다.XmlDocument로 XML 탐색은 XPath 를 사용하여 꽤 비트를 지정할 수 있습니다.특정 기준을 충족시키는 노드를 얻는 데옵니다.나무와 콘텐츠 내의 구조 측면에서 모두.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top