zoukankan      html  css  js  c++  java
  • 个人代码库のXML操作演练

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

    namespace WindowsFormsApplication1
    {
    public partial class frm_MainForm : Form
    {
    //string Filename = null;


    public frm_MainForm()
    {
    InitializeComponent();
    }

    private void btnNew_Click(object sender , EventArgs e)
    {
    SaveFileDialog sf
    =new SaveFileDialog();
    sf.Title
    = "新建XML文件 ";
    sf.Filter
    = "XML文件(*.xml)|*.xml";
    sf.ShowDialog(
    this);
    myMethod_XMLwriter(sf.FileName);

    rtb_Editor.Text
    = File.ReadAllText(sf.FileName);
    }

    private void myMethod_XMLwriter(string filename)
    {
    if ( filename != "" )
    {
    groupBox1.Enabled
    = true;

    //创建一个XML文档对象
    XmlDocument xmlDoc = new XmlDocument();

    //加入XML声明。
    xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0" , "UTF-8" , "yes"));

    //加入XML注释。
    xmlDoc.AppendChild(xmlDoc.CreateComment("注释"));

    //加入空行。
    xmlDoc.AppendChild(xmlDoc.CreateWhitespace(" \n"));

    //创建一个根节点
    //XmlElement xmlRoot = xmlDoc.CreateElement("root");
    //创建一个根节点(带命名空间)
    XmlElement xmlRoot = xmlDoc.CreateElement("YE" , "root" , "http://www.smartsoso.com");

    //添加根节点
    xmlDoc.AppendChild(xmlRoot);


    //添加【CDATA数据】。
    //xmlRoot.AppendChild(xmlDoc.CreateCDataSection("CDATA数据"));

    //创建一个子节点
    XmlElement bgcolor = xmlDoc.CreateElement("bgcolor");
    bgcolor.SetAttribute (
    "ID","yeye");
    bgcolor.InnerText
    = "red";//子节点的值
    xmlRoot.AppendChild(bgcolor);//添加到根节点中

    //保存到文件。
    xmlDoc.Save(filename);





    /*
    //读
    //创建一个XML对象
    XmlDocument myxml = new XmlDocument();

    // 读取已经有的xml
    myxml.Load(Filename);

    //声明一个节点存储根节点
    XmlNode movie = myxml.DocumentElement;

    //遍历根节点下的子节点
    foreach ( XmlNode var in movie.ChildNodes )
    {
    //Console.WriteLine(var.Name);//获取根节点的名称
    //Console.WriteLine(var.InnerText);//获取根节点的值!
    rtb_Editor.Text += var.Name;
    rtb_Editor.Text += var.InnerText;


    }
    */


    }
    }
    }
    }
    作者:Asion Tang
    凡是没有注明[转载]的文章,本Blog发表的文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    JUnit-执行测试
    JUnit注释的执行顺序
    回文数字
    JUnit简单测试
    利用jspx解决jsp后缀被限制拿shell
    wifi破解到局域网渗透
    爬虫之一:爬补天厂商数据(爬虫)
    三位一体的漏洞分析方法-web应用安全测试方法
    json.dumps错误:'utf8' codec can't decode byte解决方案
    nmap小技巧[1] 探测大网络空间中的存活主机
  • 原文地址:https://www.cnblogs.com/AsionTang/p/1885709.html
Copyright © 2011-2022 走看看