110,539
社区成员
发帖
与我相关
我的任务
分享
XmlDocument xdoc = new XmlDocument(); // 创建 xml 文档对象
XmlNode xnode = xdoc.CreateXmlDeclaration("1.0", "GB2312", "yes");
<?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>
xnode = xdoc.CreateComment(string.Format("...",???));
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);
}
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>
有没有一些好的建议呢???