博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过XmlWriterSettings类格式化XML文件的输出
阅读量:6786 次
发布时间:2019-06-26

本文共 2037 字,大约阅读时间需要 6 分钟。

 7_XmlWritingWithSettings.aspx


<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server">

    void Page_Load(object sender, EventArgs e)
    {       
       
        string xmlFilePath = @"C:\Data\Employees.xml";           
        try
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;           
            settings.ConformanceLevel = ConformanceLevel.Auto;
            settings.IndentChars = "\t";
            settings.OmitXmlDeclaration = false;
            using (XmlWriter writer = XmlWriter.Create(xmlFilePath, settings))
            {
                //Start writing the XML document
                writer.WriteStartDocument(false);    

            

                //Start with the root element
                writer.WriteStartElement("employees");                   
                    writer.WriteStartElement("employee");
                    writer.WriteAttributeString("id", "1");
                        writer.WriteStartElement("name");
                            writer.WriteElementString("firstName", "Nancy");
                            writer.WriteElementString("lastName", "lastName");
                        writer.WriteEndElement();
                        writer.WriteElementString("city", "Seattle");
                        writer.WriteElementString("state", "WA");
                        writer.WriteElementString("zipCode", "98122");                                   
                    writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();

                //Flush the object and write the XML data to the file
                writer.Flush();
                lblResult.Text = "File is written successfully";
            }
        }      
        catch (Exception ex)
        {
            lblResult.Text = "An Exception occurred: " + ex.Message;
        }       
    }

</script>

<html xmlns="" >

<head id="Head1" runat="server">
    <title>Writing XML File with XmlWriterSettings</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:label id="lblResult" runat="server" />
    </div>
    </form>
</body>
</html>

 

总结:创建了XmlWriterSettings对象的一个实例,然后设置各种属性,如Indent,ConformanceLevel,IndentChars和OmitXmlDeclaration.

            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = true;           
            settings.ConformanceLevel = ConformanceLevel.Auto;
            settings.IndentChars = "\t";
            settings.OmitXmlDeclaration = false;

然 后将XmlWriterSettings对象作为一个参数传入XmlWriter对象的Create()方法中,并将XmlWriterSettings 对象的设置的应用于新创建的XmlWriter对象。以上就是利用XmlWriterSettings对象来控制由XmlWriter对象创建的Xml文 件输出的所有操作。

 

转载于:https://www.cnblogs.com/qingtaong/archive/2012/07/02/2572814.html

你可能感兴趣的文章
UIVisualEffectView用法
查看>>
springmvc+mybatis整合cms+UC浏览器文章功能
查看>>
docker安装(centos6.5_x86_64)
查看>>
mysql悲观锁与乐观锁
查看>>
ubuntu下python2-python3版共存,创建django项目出现的问题
查看>>
2018.4.3三周第二次课
查看>>
eclipse_jee版本提供了从数据库直接生成实体类的工具!
查看>>
Error: Can't set headers after they are sent
查看>>
本地用户模式、虚拟用户模式使用
查看>>
任正非接班人亮相:原来他要的是这种类型!
查看>>
valgrind 运行出错
查看>>
ubuntu日常使用心得(随时更新中。。。)
查看>>
Java 多线程回顾
查看>>
二、nginx服务器基础配置命令
查看>>
TEMP表空间之Ogg复制进程占用
查看>>
java中的构造函数总结
查看>>
windows下kangle虚拟主机-安装mysql教程及心得
查看>>
我的友情链接
查看>>
ios中SQLite的重构封装
查看>>
centos 搭建 nagios 监控系统.
查看>>