zoukankan      html  css  js  c++  java
  • 获取XML数据

    http://www.w3school.com.cn/xml/xml_elements.asp

    <?xml version="1.0" encoding="gb2312"?>
    <string xmlns="http://tempuri.org/">200,2014/5/6 0:06:05</string>

    获取如上XML中的200,2014/5/6 0:06:05

    <?xml version="1.0" encoding="gb2312"?>
    <string xmlns="http://tempuri.org/">200,2014/5/6 0:06:05</string>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity.Validation;
    using System.Data.Entity;
    using System.Data.Objects;
    using System.Web;
    using System.Xml;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                XmlDocument xml = new XmlDocument();
                xml.Load("../../1.xml");//XML文件加载进来,路径看放的位置了,这里用了相对路径,绝对路径要好一些的
                XmlNode node = xml.LastChild;
                foreach (XmlNode item in node.ChildNodes)
                {
                    Console.WriteLine(item.InnerXml);
                    Console.WriteLine(item.InnerText);
                    //string v = item.Value;//节点的值
                    //string v1 = item.Attributes["xmlns"].Value;//节点属性值,这个获取会报错的,节点数据不对,需要其他全一些的XML数据
                    //string v2 = item["string"].Value;//元素值
                    //string v3 = item["string"].Attributes["xmlns"].Value;//元素属性值
                    //Console.WriteLine(v);
                    //Console.WriteLine(v1);
                    //Console.WriteLine(v2);
                    //Console.WriteLine(v3);                
                }
                Console.ReadKey();
            }
        }
    获取XML内容
                XmlDocument xml = new XmlDocument()
                {
                    InnerXml = @"<?xml version='1.0' encoding='gb2312'?>
    <string xmlns='http://tempuri.org/'>200,2014/5/6 0:06:05</string>"
                };
    初始化XML
    XmlDocument xml = new XmlDocument();
                xml.LoadXml(@"<?xml version='1.0' encoding='gb2312'?>
    <string xmlns='http://tempuri.org/'>200,2014/5/6 0:06:05</string>");
                Console.WriteLine(xml.InnerXml.ToString());
    初始化XML
  • 相关阅读:
    sping AOP核心思想及实现原理
    springmvc RequestMappingHandlerMapping初始化详解
    springmvc RequestMappingHandlerAdapter初始化详解
    POJ 3169 Layout
    POJ 3264
    POJ 3461 Oulipo
    二分图判定 POJ-2492
    最小生成树 prim算法
    初级BFS
    哈夫曼建树
  • 原文地址:https://www.cnblogs.com/danlis/p/5213090.html
Copyright © 2011-2022 走看看