簡體   English   中英

遍歷XmlDocument的XML子節點

[英]Looping Through XML Subnodes of an XmlDocument

我正在用C#編寫WPF應用程序,該應用程序需要顯示XML文檔中的數據。 這是XML文檔:

<Schedules>
  <Schedule>
    <Course>
      <ID>CIT160</ID>
      <Section>01</Section>
      <Name>Introduction to Programming</Name>
      <Days>TuTh</Days>
      <STime>09:30</STime>
      <ETime>11:00</ETime>
      <Location>ATHS/E246</Location>
      <Teacher>KREPSHAW, R</Teacher>
    </Course>
    <Course>
      <ID>CIT180</ID>
      <Section>01</Section>
      <Name>Introduction to Database</Name>
      <Days>MW</Days>
      <STime>12:30</STime>
      <ETime>14:00</ETime>
      <Location>ATHS/E235</Location>
      <Teacher>SINGH, M</Teacher>
    </Course>
  </Schedule>
  <Schedule>
    <Course>
      <ID>CIT160</ID>
      <Section>01</Section>
      <Name>Introduction to Programming</Name>
      <Days>TuTh</Days>
      <STime>09:30</STime>
      <ETime>11:00</ETime>
      <Location>ATHS/E246</Location>
      <Teacher>KREPSHAW, R</Teacher>
    </Course>
    <Course>
      <ID>CIT180</ID>
      <Section>25</Section>
      <Name>Introduction to Database</Name>
      <Days>MW</Days>
      <STime>17:00</STime>
      <ETime>18:30</ETime>
      <Location>ATHS/E235</Location>
      <Teacher>SINGH, M</Teacher>
    </Course>
  </Schedule>
</Schedules>

我需要輸出以下內容:

附表1

CIT160 01 Introdc ... TuTh 09:30 11:00 ATHS / E246 KREPSHAW,R

CIT180 01 Inroduc ... MW 12:30 14:00 ATHS / E235 SINGH,M

附表2

CIT160 01 Introdc ... TuTH 09:30 11:00 ATHS / E246 KREPSHAW,R

CIT180 25 Introdc ... MW 17:00 18:30 ATHS / E235 SINGH,M

我將如何遍歷XML文檔?

到目前為止,這是我所擁有的,但是工作不正確。 請幫忙。

private void LoopSchedule()
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("schedule.xml");

    XmlElement root = xmlDoc.DocumentElement;
    XmlElement subRoot = (XmlElement)root.FirstChild;
    XmlNodeList children = subRoot.ChildNodes;     

    for(int i = 0; i < children.Count; i++)
    {
        XmlElement courseRoot = (XmlElement)children[i].FirstChild;
        XmlNodeList child = courseRoot.ChildNodes;

        for(int j = 0; j < child.Count; j++)
        {
            StackPanel addPanel = new StackPanel();
            Label lblChild = new Label();
            lblChild.Foreground = new SolidColorBrush(Colors.White);
            lblChild.Content = child[j].Name;

            addPanel.Children.Add(lblChild);
            stkPan_display.Children.Add(addPanel);
        }
    }
}

這里有幾點:

  1. 您這里需要三個循環,而不是兩個。 一個用於日程表,一個用於每個日程表中的課程,一個用於每個課程中的數據元素。
  2. 如果需要在輸出中,則需要創建一個堆棧面板並為計划號添加標簽。 我的代碼中沒有這樣的代碼。
  3. 為課程行創建堆棧面板時,應在課程子項的循環之外創建它,然后在循環中為其添加每個標簽。 每個課程只需要一個StackPanel,而每個孩子只需要一個。
  4. 您需要將行的StackPanel方向設置為Horizo​​ntal; 默認為“垂直”。 另外,如果將背景設置為深色,則只能將畫筆顏色設置為白色,否則將無法看到它。 默認背景為白色。

請嘗試以下代碼:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("schedule.xml");

XmlElement root = xmlDoc.DocumentElement;  // Schedules

int scheduleNumber = 0;
foreach (XmlElement schedule in root.ChildNodes)
{
    scheduleNumber++;
    StackPanel schedulePanel = new StackPanel { Orientation = Orientation.Horizontal };
    Label scheduleLabel = new Label { Content = "Schedule " + scheduleNumber.ToString() };
    schedulePanel.Children.Add(scheduleLabel);
    stkPan_display.Children.Add(schedulePanel);

    foreach (XmlElement course in schedule.ChildNodes)
    {
        StackPanel addPanel = new StackPanel { Orientation = Orientation.Horizontal };

        // all child elements of the Course element
        foreach (XmlElement child in course.ChildNodes)
        {
            Label lblChild = new Label();
            lblChild.Foreground = new SolidColorBrush(Colors.Black);
            lblChild.Content = child.InnerText;
            addPanel.Children.Add(lblChild);
        }

        stkPan_display.Children.Add(addPanel);
    }
}

使用xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var schedules = doc.Descendants("Schedule").Select(x => new {
                courses = x.Elements("Course").Select(y => new {
                    id = (string)y.Element("ID"),
                    section = (string)y.Element("Section"),
                    name = (string)y.Element("Name"),
                    days = (string)y.Element("Days"),
                    stime = ((DateTime)y.Element("STime")).TimeOfDay,
                    etime = ((DateTime)y.Element("ETime")).TimeOfDay,
                    location = (string)y.Element("Location"),
                    teacher = (string)y.Element("Teacher")
                }).ToList()
            }).ToList();

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM