如何从数据库中查找出数据并写入固定的XML格式
必须是每次写入xml时,都是自动新建一个xml文件。 
请高手讲解?????????? 已经困惑很久,一直没得到解决

解决方案 »

  1.   

    .net有好几个XML控件的,操作应该很方便,只是我没有仔细研究过
      

  2.   

    我用过DATASET,直接生成了一个用select字段命名的XML文件,而不是自动新建一个XML格式的文件
      

  3.   

    你的意思是不是
    我去数据库取出来
    之后按照取出来的表
    然后写难道XML里面吗
      

  4.   

    是的,首先从数据库表里取到数据,然后写入固定的XML格式文件中,并且每次写入的时候都自动新建一个XML文件,有人说用以下
    using System.IO;            
    //-------------------------
                StreamWriter sw = new StreamWriter("文件名,如xml.xml");
                //当指定的文件名存在的时,就重写文件,如果不存,就创建!
                sw.write("将xml内容写进来");
                sw.close();
      

  5.   


       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();
          }
        } 
      

  6.   

    <?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);