Csharp/CSharp Tutorial/XML/XmlElement

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Get node children count

using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;
    class Program{
        static void Main(string[] args)
        {
            XmlDocument itemDoc = new XmlDocument();
            itemDoc.Load("items.xml");
            Console.WriteLine("DocumentElement has {0} children.",itemDoc.DocumentElement.ChildNodes.Count);
            foreach (XmlNode itemNode in itemDoc.DocumentElement.ChildNodes)
            {
                XmlElement itemElement = (XmlElement)itemNode;
                Console.WriteLine("\n[Item]: {0}\n{1}", itemElement.Attributes["name"].Value,itemElement.Attributes["description"].Value);
                if (itemNode.ChildNodes.Count == 0)
                    Console.WriteLine("(No additional Information)\n");
                else
                {
                    foreach (XmlNode childNode in itemNode.ChildNodes)
                    {
                        if (childNode.Name.ToUpper() == "ATTRIBUTE")
                        {
                            Console.WriteLine("{0} : {1}",
                                childNode.Attributes["name"].Value,
                                childNode.Attributes["value"].Value);
                        }
                        else if (childNode.Name.ToUpper() == "SPECIALS")
                        {
                            foreach (XmlNode specialNode in childNode.ChildNodes)
                            {
                                Console.WriteLine("*{0}:{1}",
                                    specialNode.Attributes["name"].Value,
                                    specialNode.Attributes["description"].Value);
                            }
                        }
                    }
                }
            }
        }
    }

Output child node inner text

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
    public class MainClass
    {
        public static void Main()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("employees.xml");
            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                string employeeid = node.Attributes["employeeid"].Value;
                Console.WriteLine(employeeid);
            }
            XmlElement ele= doc.GetElementById("id");
            Console.WriteLine(ele.ChildNodes[0].InnerText);
            Console.WriteLine(ele.ChildNodes[1].InnerText);
            Console.WriteLine(ele.ChildNodes[2].InnerText);
            Console.WriteLine(ele.ChildNodes[3].InnerText);
        }
    }

Selecting single Node with SelectSingleNode as XmlElement

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public override string ToString()
    {
        return string.Format("{0} {1}\nEmail:   {2}", FirstName, LastName, EmailAddress);
    }
}
public class Tester
{
    static void Main()
    {
        List<Customer> customers = new List<Customer>{
                new Customer { FirstName = "A", 
                               LastName = "G",
                               EmailAddress = "o@a.ru"},
                new Customer { FirstName = "B", 
                               LastName = "C",
                               EmailAddress = "k@a.ru" },
                new Customer { FirstName = "D", 
                               LastName = "C",
                               EmailAddress = "d@a.ru" },
                new Customer { FirstName = "E", 
                               LastName = "G",
                               EmailAddress = "j@a.ru" },
                new Customer { FirstName = "L", 
                               LastName = "H",
                               EmailAddress = "l@a.ru" }
            };
        XmlDocument customerXml = new XmlDocument();
        XmlElement rootElem = customerXml.CreateElement("Customers");
        customerXml.AppendChild(rootElem);
        foreach (Customer customer in customers)
        {
            XmlElement customerElem = customerXml.CreateElement("Customer");
            XmlAttribute firstNameAttr = customerXml.CreateAttribute("FirstName");
            firstNameAttr.Value = customer.FirstName;
            customerElem.Attributes.Append(firstNameAttr);
            XmlAttribute lastNameAttr = customerXml.CreateAttribute("LastName");
            lastNameAttr.Value = customer.LastName;
            customerElem.Attributes.Append(lastNameAttr);
            XmlElement emailAddress = customerXml.CreateElement("EmailAddress");
            emailAddress.InnerText = customer.EmailAddress;
            customerElem.AppendChild(emailAddress);
            rootElem.AppendChild(customerElem);
        }

        string xPath = "/Customers/Customer[@FirstName="D"]";
        XmlElement customerElem1 = customerXml.SelectSingleNode(xPath) as XmlElement;
        if (customerElem1 != null)
        {
            Console.WriteLine(customerElem1.OuterXml);
            Console.WriteLine("customerElem.HasAttributes = {0}", customerElem1.HasAttributes);
        }
        else
            Console.WriteLine("Not found");
    }
}