C# XMLファイルを読み込んでTreeViewに表示させる。


スポンサーリンク


C#でXMLを操作するためには、System.Xml.XmlDocumentクラスを使う。
このクラスはメモリにXMLを全て取り込むため、巨大なXMLファイルの読み込みには向いていない。

巨大はXMLファイルはXmlReaderやXmlWriterを使う。

以下のpom.xmlを読み込んで、TreeViewに表示させてみる。

f:id:sho322:20151124113352j:plain

<?xml version="1.0" encoding="utf-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tripod.demo</groupId>
  <artifactId>Karthik-project</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Karthik-project Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <properties>
    <jboss-as.deploy.hostname>localhost</jboss-as.deploy.hostname>
    <!-- Where to deploy. -->
    <jboss-as.deploy.user>admin</jboss-as.deploy.user>
    <jboss-as.deploy.pass>admin</jboss-as.deploy.pass>
    <plugin.war.warName>target/Karthik-project.war</plugin.war.warName>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
          <warName>target/Karthik-project.war</warName>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.jboss.as.plugins</groupId>
        <artifactId>jboss-as-maven-plugin</artifactId>
        <version>7.3.Final</version>
        <configuration>
          <hostname>localhost</hostname>
          <username>admin</username>
          <password>admin</password>
          <jbossHome>/home/smadugula/jboss-eas-5.2</jbossHome>
          <serverName>Standalone</serverName>
          <targetDir>target</targetDir>
          <fileName>target/Karthik-project.war</fileName>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

コードはこれ。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace myXmlSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtXmlFile.Text = Path.Combine(Application.StartupPath, @"../../pom.xml");
        }

        private void xmlReadButton_Click(object sender, EventArgs e)
        {
            treeXml.Nodes.Clear();
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(txtXmlFile.Text);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return;
            }

            ConvertXmlNodeToTreeNode(doc, treeXml.Nodes);
            treeXml.Nodes[0].ExpandAll();
        }

        private void ConvertXmlNodeToTreeNode(XmlNode xmlNode, TreeNodeCollection treeNodes)
        {
            TreeNode newTreeNode = treeNodes.Add(xmlNode.Name);
            switch (xmlNode.NodeType)
            {
                case XmlNodeType.ProcessingInstruction:
                case XmlNodeType.XmlDeclaration:
                    newTreeNode.Text = "<?" + xmlNode.Name + ">";
                    break;
                case XmlNodeType.Element:
                    newTreeNode.Text = "<" + xmlNode.Name + ">";
                    break;
                case XmlNodeType.Attribute:
                    newTreeNode.Text = "ATRIBUTE:" + xmlNode.Name;
                    break;
                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                    newTreeNode.Text = xmlNode.Value;
                    break;
                case XmlNodeType.Comment:
                    newTreeNode.Text = "<!--" + xmlNode.Value + "-->";
                    break;
            }


            if (xmlNode.Attributes != null)
            {
                foreach (XmlAttribute attribute in xmlNode.Attributes)
                {
                    ConvertXmlNodeToTreeNode(attribute, newTreeNode.Nodes);
                }
            }

            foreach (XmlNode childNode in xmlNode.ChildNodes)
            {
                ConvertXmlNodeToTreeNode(childNode, newTreeNode.Nodes);
            }
        }


    }
}

基礎からわかる C#

基礎からわかる C#