C#DataSet.tables怎么定义XML编码以及保存注释

茗香淡然 2014-04-02 10:43:20
以前的XML文档都是用 XmlDocument 创建的 可以加入


XmlDocument xdoc = new XmlDocument(); // 创建 xml 文档对象
XmlNode xnode = xdoc.CreateXmlDeclaration("1.0", "GB2312", "yes");

但是我没有找到 datatable 对 XML文档编码定义的方法。
WriteXml 方法没有文档编码的定义,变成了下面这样的。
<?xml version="1.0" standalone="true"?>

我想要的是怎么在修改后按原文格式保存回去。
如原文:

<?xml version="1.0" encoding="GB2312" standalone="true"?>
<!--注释内容-->
-<SysString>
-<toTable>
<ID>1</ID>
<姓名>Null</姓名>
</toTable>
</SysString>


修改后格式:

<?xml version="1.0" encoding="GB2312" standalone="true"?>
<!--注释内容-->
-<SysString>
-<toTable>
<ID>1</ID>
<姓名>张三</姓名>
</toTable>
-<toTable>
<ID>2</ID>
<姓名>李四</姓名>
</toTable>
</SysString>

修改后要怎么样才能保持原来的格式呢?

最好上段代码...学习中
每天回一贴,养成好习惯!!!
...全文
219 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
茗香淡然 2014-04-18
  • 打赏
  • 举报
回复
引用 6 楼 lyq8376 的回复:

public static void WriteDataTable(DataTable dt, string[] innerComment, string setpath)
        {
            //内容区域段注释数和列数不同,则不处理内容区域段注释
            bool hasInnerComment = true;
            if (innerComment.Length != dt.Columns.Count)
            {
                hasInnerComment = false;
            }
            XmlDocument xdoc = new XmlDocument(); // 创建 xml 文档对象
            XmlNode xnode = xdoc.CreateXmlDeclaration("1.0", "GB2312", "yes");
            xdoc.AppendChild(xnode);
            xnode = xdoc.CreateComment("请勿修改下面的内容,否则程序将出错!");
            xdoc.AppendChild(xnode);
            XmlNode root = xdoc.CreateElement("SysString");
            foreach (DataRow dr in dt.Rows)
            {
                XmlNode parent = xdoc.CreateElement("toTable");
                foreach (DataColumn dc in dt.Columns)
                {
                    if (hasInnerComment)
                    {
                        XmlComment comm = xdoc.CreateComment(innerComment[dc.Ordinal]);
                        parent.AppendChild(comm);
                    }
                    XmlNode child = xdoc.CreateElement(dc.ColumnName);
                    child.InnerText = dr[dc.ColumnName].ToString();
                    parent.AppendChild(child);               
                }
                root.AppendChild(parent);
            }
            xdoc.AppendChild(root);
            xdoc.Save(setpath);
        }
先结贴...有不明白的再找你请教一下.呵呵!!!
茗香淡然 2014-04-15
  • 打赏
  • 举报
回复
引用 6 楼 lyq8376 的回复:

public static void WriteDataTable(DataTable dt, string[] innerComment, string setpath)
        {
            //内容区域段注释数和列数不同,则不处理内容区域段注释
            bool hasInnerComment = true;
            if (innerComment.Length != dt.Columns.Count)
            {
                hasInnerComment = false;
            }
            XmlDocument xdoc = new XmlDocument(); // 创建 xml 文档对象
            XmlNode xnode = xdoc.CreateXmlDeclaration("1.0", "GB2312", "yes");
            xdoc.AppendChild(xnode);
            xnode = xdoc.CreateComment("请勿修改下面的内容,否则程序将出错!");
            xdoc.AppendChild(xnode);
            XmlNode root = xdoc.CreateElement("SysString");
            foreach (DataRow dr in dt.Rows)
            {
                XmlNode parent = xdoc.CreateElement("toTable");
                foreach (DataColumn dc in dt.Columns)
                {
                    if (hasInnerComment)
                    {
                        XmlComment comm = xdoc.CreateComment(innerComment[dc.Ordinal]);
                        parent.AppendChild(comm);
                    }
                    XmlNode child = xdoc.CreateElement(dc.ColumnName);
                    child.InnerText = dr[dc.ColumnName].ToString();
                    parent.AppendChild(child);               
                }
                root.AppendChild(parent);
            }
            xdoc.AppendChild(root);
            xdoc.Save(setpath);
        }
不过你到是提醒我可以加入行的时候加入注释....感觉上还是有点模糊,先想想
 xnode = xdoc.CreateComment(string.Format("...",???));
茗香淡然 2014-04-15
  • 打赏
  • 举报
回复
        private void dsta()
        {
            string setpaths = "terminal.xml";
            System.Data.DataSet ds = new System.Data.DataSet("SysString");
            System.Data.DataTable dt = new System.Data.DataTable("toTable");
            //ds.ReadXml(setpaths);

            DataColumn intIdColumn = new DataColumn();
            intIdColumn.DataType = System.Type.GetType("System.Int32");
            intIdColumn.ColumnName = "ID";
            intIdColumn.AutoIncrementSeed = 1;
            intIdColumn.AutoIncrement = true;
            dt.Columns.Add(intIdColumn);

            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Sex", typeof(string));
            dt.Columns.Add("Age", typeof(Int32));

            intIdColumn = new DataColumn();
            intIdColumn.DataType = System.Type.GetType("System.Boolean");
            intIdColumn.ColumnName = "真假";
            intIdColumn.DefaultValue = "false";
            dt.Columns.Add(intIdColumn);

            XmlDocument xmldoc = new XmlDocument();
            try
            {   ///导入xml文档
                xmldoc.Load(setpaths);
                XmlNode node = xmldoc.SelectSingleNode("SysString");
                if (node != null)
                    ///读取<centerInfo>的节点
                    foreach (XmlNode xnode in xmldoc.SelectNodes("SysString/toTable"))
                    {   ///创建一个新行
                        DataRow row1 = dt.NewRow();
                        ///读取节点数据,并填充数据行
                        foreach (XmlNode xcnode in xnode.ChildNodes)
                        {
                            row1[xcnode.Name] = xcnode.InnerText;
                        }
                        ///添加该数据行
                        dt.Rows.Add(row1);
                    }
                ds.Tables.Add(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            dataGridView1.DataSource = ds.Tables[0];
            try
            {
                System.Data.DataRow row = dt.NewRow();
                row["Name"] = "kimi";
                row["Sex"] = "male";
                row["Age"] = "20"; 
                dt.Rows.Add(row);
            }
            catch (Exception exo) { MessageBox.Show(exo.ToString()); }
            try
            {
                if (ds != null)
                {
                    FileStream fs = new FileStream(setpaths, FileMode.Create);
                    XmlTextWriter xtr = new XmlTextWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
 
                    xtr.Formatting = Formatting.Indented;
                    xtr.Indentation = 6;

                    xtr.WriteStartDocument(true);
                    xtr.WriteComment("请勿修改下面的内容,否则程序将出错!!");
                    ds.WriteXml(xtr, XmlWriteMode.IgnoreSchema);
                    xtr.Close();
                    fs.Close();
                }
            }
            catch (Exception exc) { MessageBox.Show(exc.ToString()); }
        }
其实我想要的效果是空表带上旧表,也就是说把新表的类型带出来,让旧表有类型效果.在DataGridView显示的时候可以看到类型如Boolean,Int32, 像上面的表中ID列是Int32类型,自增值...真假列Boolean 类型...如果是通过直接读取的话,添加新行就要手动去添加. 所以说,显示类型是首选,其次才是注释. http://bbs.csdn.net/topics/390749932 我提问过的帖子
  • 打赏
  • 举报
回复

public static void WriteDataTable(DataTable dt, string[] innerComment, string setpath)
        {
            //内容区域段注释数和列数不同,则不处理内容区域段注释
            bool hasInnerComment = true;
            if (innerComment.Length != dt.Columns.Count)
            {
                hasInnerComment = false;
            }
            XmlDocument xdoc = new XmlDocument(); // 创建 xml 文档对象
            XmlNode xnode = xdoc.CreateXmlDeclaration("1.0", "GB2312", "yes");
            xdoc.AppendChild(xnode);
            xnode = xdoc.CreateComment("请勿修改下面的内容,否则程序将出错!");
            xdoc.AppendChild(xnode);
            XmlNode root = xdoc.CreateElement("SysString");
            foreach (DataRow dr in dt.Rows)
            {
                XmlNode parent = xdoc.CreateElement("toTable");
                foreach (DataColumn dc in dt.Columns)
                {
                    if (hasInnerComment)
                    {
                        XmlComment comm = xdoc.CreateComment(innerComment[dc.Ordinal]);
                        parent.AppendChild(comm);
                    }
                    XmlNode child = xdoc.CreateElement(dc.ColumnName);
                    child.InnerText = dr[dc.ColumnName].ToString();
                    parent.AppendChild(child);               
                }
                root.AppendChild(parent);
            }
            xdoc.AppendChild(root);
            xdoc.Save(setpath);
        }
茗香淡然 2014-04-14
  • 打赏
  • 举报
回复
引用 4 楼 lyq8376 的回复:
楼主,要在在内容区域段中加注释,就不要使用WriteXml 方法,可以改为使用XmlDocument的相关类
具体如何做呢?
  • 打赏
  • 举报
回复
楼主,要在在内容区域段中加注释,就不要使用WriteXml 方法,可以改为使用XmlDocument的相关类
茗香淡然 2014-04-13
  • 打赏
  • 举报
回复

               if (ds != null)
                {
                    FileStream fs = new FileStream(setpaths, FileMode.Create);
                    XmlTextWriter xtr = new XmlTextWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
                    
                    xtr.Formatting = Formatting.Indented;
                    xtr.Indentation = 6;
                    xtr.WriteStartDocument(true);  //独立声明
                    //xtr.WriteStartDocument();
                    xtr.WriteComment("请勿修改下面的内容,否则程序将出错!!");
                    //↑↑↑↑↑↑↑ 只能出现在声明之下,根节点之上,  在内容区域段中怎么加注释呢? ↑↑↑↑↑
                    ds.WriteXml(xtr, XmlWriteMode.IgnoreSchema);
                    xtr.Close();
                    fs.Close();
                    //ds.Clear();
                    //ds.Dispose();

                }
<?xml version="1.0" encoding="GB2312" standalone="true"?>
<!--请勿修改下面的内容,否则程序将出错!!-->
-<SysString>
 -<toTable>
 <ID>1</ID>
 <Name>wqqwwq</Name>
 <Sex>male</Sex> 
<Age>20</Age> 
<真假>false</真假>
 </toTable>
 -<toTable> 
<ID>2</ID>
 <Name>kimi</Name>
 <Sex>male</Sex> 
<Age>20</Age>
 <真假>false</真假> 
</toTable>
</SysString> 
有没有一些好的建议呢???
茗香淡然 2014-04-03
  • 打赏
  • 举报
回复
其实自己有个思路,就是不太成熟。。。o(︶︿︶)o~唉,先自己弄弄。。。
茗香淡然 2014-04-03
  • 打赏
  • 举报
回复
早上起来一看,没人回答

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧