如何从数据库中查找出数据并写入固定的XML格式,并自动新建一个XML文件 如何从数据库中查找出数据并写入固定的XML格式必须是每次写入xml时,都是自动新建一个xml文件。 请高手讲解?????????? 已经困惑很久,一直没得到解决 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 .net有好几个XML控件的,操作应该很方便,只是我没有仔细研究过 我用过DATASET,直接生成了一个用select字段命名的XML文件,而不是自动新建一个XML格式的文件 你的意思是不是我去数据库取出来之后按照取出来的表然后写难道XML里面吗 是的,首先从数据库表里取到数据,然后写入固定的XML格式文件中,并且每次写入的时候都自动新建一个XML文件,有人说用以下using System.IO; //------------------------- StreamWriter sw = new StreamWriter("文件名,如xml.xml"); //当指定的文件名存在的时,就重写文件,如果不存,就创建! sw.write("将xml内容写进来"); sw.close(); using System; using System.Data; using System.IO; using System.Xml; using System.Text; // 相应C#代码: private string ConvertDataTableToXML(DataTable xmlDS) { MemoryStream stream = null; XmlTextWriter writer = null; try { stream = new MemoryStream(); writer = new XmlTextWriter(stream, Encoding.Default); xmlDS.WriteXml(writer); int count = (int)stream.Length; byte[] arr = new byte[count]; stream.Seek(0, SeekOrigin.Begin); stream.Read(arr, 0, count); UTF8Encoding utf = new UTF8Encoding(); return utf.GetString(arr).Trim(); } catch { return String.Empty; } finally { if (writer != null) writer.Close(); } } private DataSet ConvertXMLToDataSet(string xmlData) { StringReader stream = null; XmlTextReader reader = null; try { DataSet xmlDS = new DataSet(); stream = new StringReader(xmlData); reader = new XmlTextReader(stream); xmlDS.ReadXml(reader); return xmlDS; } catch (Exception ex) { string strTest = ex.Message; return null; } finally { if (reader != null) reader.Close(); } } <?xml version="1.0" encoding="UTF-8"?><papers> <paper title="java与模式问卷调查" name="book.xml" answerCount="0"> <problem id="1" aCount="0" bCount="0" cCount="0" dCount="0" /> <problem id="2" aCount="0" bCount="0" cCount="0" dCount="0" /> <problem id="3" aCount="0" bCount="0" cCount="0" dCount="0" /> <problem id="4" aCount="0" bCount="0" cCount="0" dCount="0" /> </paper> <paper title="程序员问卷调查" name="person.xml" answerCount="0"> <problem id="1" aCount="0" bCount="0" cCount="0" dCount="0" /> <problem id="2" aCount="0" bCount="0" cCount="0" dCount="0" /> <problem id="3" aCount="0" bCount="0" cCount="0" dCount="0" /> <problem id="4" aCount="0" bCount="0" cCount="0" dCount="0" /> </paper> <paper title="产品问卷调查" name="test.xml" answerCount="0"> <problem id="1" aCount="0" bCount="0" cCount="0" dCount="0" /> <problem id="2" aCount="0" bCount="0" cCount="0" dCount="0" /> <problem id="3" aCount="0" bCount="0" cCount="0" dCount="0" /> </paper></papers>我自己刚刚好做了一个。这种xml格式的,代码如下,还要引入一个jar包DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element root = document.createElement("papers"); for(Paper paper:keys){ System.out.println(paper); Element paperE = document.createElement("paper"); Element problemE = document.createElement("problem"); Attr name = document.createAttribute("name"); Attr title = document.createAttribute("title"); Attr answerC = document.createAttribute("answerCount"); name.setNodeValue(paper.getPaperName()); title.setNodeValue(paper.getPaperTitle()); answerC.setNodeValue(paper.getAnswerCount()+""); paperE.setAttributeNode(name); paperE.setAttributeNode(title); paperE.setAttributeNode(answerC); Map<Integer, Answer> answerMap = map.get(paper); System.out.println(answerMap); Set<Integer> key=answerMap.keySet(); for(Integer id:key){ Attr idStr = document.createAttribute("id"); idStr.setNodeValue(id+""); Answer answer = answerMap.get(id); System.out.println(answer); Attr aCount = document.createAttribute("aCount"); Attr bCount = document.createAttribute("bCount"); Attr cCount = document.createAttribute("cCount"); Attr dCount = document.createAttribute("dCount"); aCount.setNodeValue(answer.getaCount()+""); bCount.setNodeValue(answer.getbCount()+""); cCount.setNodeValue(answer.getcCount()+""); dCount.setNodeValue(answer.getdCount()+""); } paperE.appendChild(problemE); root.appendChild(paperE); } document.appendChild(root); FileOutputStream fos = new FileOutputStream(new File("src/design/test.xml")); ((org.apache.crimson.tree.XmlDocument)document).write(fos); 怎样给指定的DataGridView里的单元格 画下划线? 两个winform窗体间的控件是否可以互相访问? 请教一小段VB.NET代码如何转为C#的 datalist与checkbox判断问题 我找的代码组合一起的,大家帮忙怎么把这数据组合,一个函数里的数据源在令个函数里怎么调起来 要开发一个项目,现在正考虑使用什么方法,各位大侠指点下 C#控件listview的问题!高手帮帮忙! 请教:如何把DbType转换为SqlDbType? 如何实现*和?的搜索 给出日期,如何计算这个日期是本年的第几个周? 使用反射执行WCF接口,int型得到的结果总是0 未将对象引用设置到对象的实例?????
我去数据库取出来
之后按照取出来的表
然后写难道XML里面吗
using System.IO;
//-------------------------
StreamWriter sw = new StreamWriter("文件名,如xml.xml");
//当指定的文件名存在的时,就重写文件,如果不存,就创建!
sw.write("将xml内容写进来");
sw.close();
using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Text;
// 相应C#代码:
private string ConvertDataTableToXML(DataTable xmlDS)
{
MemoryStream stream = null;
XmlTextWriter writer = null;
try
{
stream = new MemoryStream();
writer = new XmlTextWriter(stream, Encoding.Default);
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
UTF8Encoding utf = new UTF8Encoding();
return utf.GetString(arr).Trim();
}
catch
{
return String.Empty;
}
finally
{
if (writer != null) writer.Close();
}
}
private DataSet ConvertXMLToDataSet(string xmlData)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
DataSet xmlDS = new DataSet();
stream = new StringReader(xmlData);
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
return xmlDS;
}
catch (Exception ex)
{
string strTest = ex.Message;
return null;
}
finally
{
if (reader != null)
reader.Close();
}
}
<papers>
<paper title="java与模式问卷调查" name="book.xml" answerCount="0">
<problem id="1" aCount="0" bCount="0" cCount="0" dCount="0" />
<problem id="2" aCount="0" bCount="0" cCount="0" dCount="0" />
<problem id="3" aCount="0" bCount="0" cCount="0" dCount="0" />
<problem id="4" aCount="0" bCount="0" cCount="0" dCount="0" />
</paper>
<paper title="程序员问卷调查" name="person.xml" answerCount="0">
<problem id="1" aCount="0" bCount="0" cCount="0" dCount="0" />
<problem id="2" aCount="0" bCount="0" cCount="0" dCount="0" />
<problem id="3" aCount="0" bCount="0" cCount="0" dCount="0" />
<problem id="4" aCount="0" bCount="0" cCount="0" dCount="0" />
</paper>
<paper title="产品问卷调查" name="test.xml" answerCount="0">
<problem id="1" aCount="0" bCount="0" cCount="0" dCount="0" />
<problem id="2" aCount="0" bCount="0" cCount="0" dCount="0" />
<problem id="3" aCount="0" bCount="0" cCount="0" dCount="0" />
</paper>
</papers>
我自己刚刚好做了一个。这种xml格式的,代码如下,还要引入一个jar包
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element root = document.createElement("papers");
for(Paper paper:keys){
System.out.println(paper);
Element paperE = document.createElement("paper");
Element problemE = document.createElement("problem");
Attr name = document.createAttribute("name");
Attr title = document.createAttribute("title");
Attr answerC = document.createAttribute("answerCount");
name.setNodeValue(paper.getPaperName());
title.setNodeValue(paper.getPaperTitle());
answerC.setNodeValue(paper.getAnswerCount()+"");
paperE.setAttributeNode(name);
paperE.setAttributeNode(title);
paperE.setAttributeNode(answerC);
Map<Integer, Answer> answerMap = map.get(paper);
System.out.println(answerMap);
Set<Integer> key=answerMap.keySet();
for(Integer id:key){
Attr idStr = document.createAttribute("id");
idStr.setNodeValue(id+"");
Answer answer = answerMap.get(id);
System.out.println(answer);
Attr aCount = document.createAttribute("aCount");
Attr bCount = document.createAttribute("bCount");
Attr cCount = document.createAttribute("cCount");
Attr dCount = document.createAttribute("dCount");
aCount.setNodeValue(answer.getaCount()+"");
bCount.setNodeValue(answer.getbCount()+"");
cCount.setNodeValue(answer.getcCount()+"");
dCount.setNodeValue(answer.getdCount()+"");
}
paperE.appendChild(problemE);
root.appendChild(paperE);
}
document.appendChild(root);
FileOutputStream fos = new FileOutputStream(new File("src/design/test.xml"));
((org.apache.crimson.tree.XmlDocument)document).write(fos);