我从数据库中多个表查询的数据最后生成一个字符串,他是完整的xml文件数据,如何判断相应的xml文件在系统中是否存在,如果不存在,创建一个xml文件,然后将这个字符串的数据写到xml文件中。

解决方案 »

  1.   

    可以参考下面代码:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim xtwMyData As System.Xml.XmlTextWriter = Nothing
            Dim intCurrRow As Integer
            Dim intNumRows As Integer
            'myDataBind()
            xtwMyData = New System.Xml.XmlTextWriter("c:\Test.XML", Nothing)
            intNumRows = ds.Tables(0).Rows.Count - 1        With xtwMyData
                .WriteStartDocument(False)
                .Formatting = System.Xml.Formatting.Indented
                .WriteDocType("Names", Nothing, Nothing, Nothing)
                .WriteComment("This file represents names list")
                .WriteStartElement("names")            For intCurrRow = 0 To intNumRows
                    '-- Start the current row
                    .WriteStartElement("name", Nothing)                '-- Write the fields
                    .WriteAttributeString("ApplicationID", ds.Tables(0).Rows(intCurrRow).Item("ApplicationID"))
                    .WriteAttributeString("Version", ds.Tables(0).Rows(intCurrRow).Item("Version"))
                    .WriteAttributeString("UserID", ds.Tables(0).Rows(intCurrRow).Item("UserID"))
                    .WriteAttributeString("ErrorMessage", ds.Tables(0).Rows(intCurrRow).Item("ErrorMessage"))
                    .WriteAttributeString("Path", ds.Tables(0).Rows(intCurrRow).Item("Path"))
                    .WriteAttributeString("Comments", ds.Tables(0).Rows(intCurrRow).Item("Comments"))
                    .WriteAttributeString("State", ds.Tables(0).Rows(intCurrRow).Item("State"))
                    .WriteAttributeString("DateUpdated", ds.Tables(0).Rows(intCurrRow).Item("DateUpdated"))
                    '-- Ending the row 
                    .WriteEndElement()            Next            '-- Write the XML to file and close the writer
                .Flush()
                .Close()
            End With
        End Sub
      

  2.   

    1. XML 文档操作 
      1.1 加载一个XML文档 
      Dim objDoc As New XmlDocument() 
      '-- Load xml document Sample.xml 
      objDoc.Load("Sample.xml") 
       
      1.2 加载一个XML数据 
       Dim objDoc As New XmlDocument() 
       Dim strXML As String 
       strXML = "<Employees><Employee id=”12345”><Employee_ID>12345</Employee_ID><Name>Zhang Bin</Name></Employee></Employees>" 
       
       '-- Load xml data 
       objDoc.Load(strXML) 
       
      1.3 保存文档 
      '—Save xml document 
      objDoc.Save("Sample.xml") 
       
      2 XML 数据的查询 
      最常见的XML数据类型有:Element, Attribute,Comment, Text. 
      Element, 指形如<Name>Tom<Name>的节点。它可以包括:Element, Text, Comment, ProcessingInstruction, CDATA, and EntityReference. 
      Attribute, 指在<Employee id=”12345”>中的粗体部分。 
      Comment,指形如:<!-- my comment --> 的节点。 
      Text,指在<Name>Tom<Name>的粗体部分。 
      在XML中,可以用XmlNode对象来参照各种XML数据类型。 
      2.1 查询已知绝对路径的节点(集) 
      objNodeList = objDoc.SelectNodes(“Company/Department/Employees/Employee”) 
      或者 
      objNodeList = objNode.SelectNodes(“/Company/Department/Employees/Employee”) 
      以上两种方法可返回一个NodeList对象,如果要返回单个节点可使用SelectSingleNode方法,该方法如果查询到一个或多个节点,返回第一个节点;如果没有查询的任何节点返回 Nothing。例如: 
      objNode = objNode.SelectSingleNode(“/Company/Department/Employees/Employee”) 
      If Not (objNode is Nothing) then 
       ‘- Do process 
      End If 
      2.2 查询已知相对路径的节点(集) 
      可使用类似于文件路径的相对路径的方式来查询XML的数据 
      objNode = objDoc.SelectSingleNode(“Company/Department”) 
      objNodeList = objNode.SelectNodes(“../Department) 
      objNode = objNode.SelectNode(“Employees/Employee”) 
      2.3 查询已知元素名的节点(集) 
      在使用不规则的层次文档时,由于不知道中间层次的元素名,可使用//符号来越过中间的节点,查询其子,孙或多层次下的其他所有元素。例如: 
      objNodeList = objDoc.SelectNodes(“Company//Employee”) 
      2.4 查询属性(attribute)节点 
      以上的各种方法都返回元素(element)节点(集),返回属性(attribute),只需要采用相应的方法,在属性名前加一个@符号即可,例如: 
      objNodeList = objDoc.SelectNodes(“Company/Department/Employees/Employee/@id”) 
      objNodeList = objDoc.SelectNodes(“Company//@id”) 
      2.5 查询Text节点 
      使用text()来获取Text节点。 
      objNode = objDoc.SelectSingleNode(“Company/Department/Deparmt_Name/text()”) 
      2.6 查询特定条件的节点 
      使用[]符号来查询特定条件的节点。例如: 
      a. 返回id号为 10102的Employee节点 
       objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee[@id=’10102’]”) 
      b. 返回Name为Zhang Qi的Name 节点 
       objNode = objDoc.SelectSingleNode(“Company/Department/Employees/Employee/Name[text()=’Zhang Qi’]”) 
      c. 返回部门含有职员22345的部门名称节点 
      objNode = objDoc.SelectSingleNode("Company/Department[Employees/Employee/@id='22345']/Department_Name") 
      2.7 查询多重模式的节点 
      使用 | 符号可以获得多重模式的节点。例如: 
      objNodeList = objDoc.SelectNodes(“Company/Department/Department_Name | Company/Department/Manager”) 
      2.8 查询任意子节点 
      使用*符号可以返回当前节点的所有子节点。 
      objNodeList = objDoc.SelectNodes(“Company/*/Manager) 
      或者 
      objNodeList = objNode.ChildNodes 
       
      3 XML数据的编辑 
      3.1 增加一个元素的属性(attribute)节点 
      Dim objNodeAttr As XmlNode 
       objNodeAttr = objDoc.CreateAttribute("id", Nothing) 
       objNodeAttr.InnerXml = "101" 
      objNode.Attributes.Append(objNodeAttr) 
      3.2 删除一个元素的属性 
      objNode.Attributes.Remove(objNodeAttr) 
      3.3 增加一个子元素(Element) 
      Dim objNodeChild As XmlNode 
      objNodeChild = objDoc.CreateElement(Nothing, "ID", Nothing) 
      objNodeChild.InnerXml = "101" 
      objNode.AppendChild(objNodeChild) 
      3.4 删除一个子元素 
      objNode.RemoveChild(objNodeChild) 
      3.5 替换一个子元素 
      objNOde.ReplaceChild(newChild,oldChild)
      

  3.   

    搞定了,谢谢!
    FileStream fs =  File.Open(fileName, FileMode.Create ,FileAccess.Write,FileShare.Write )    {                            
         StreamWriter writer = new StreamWriter(fs);
         writer.Write(YourXMLStringHere);
         writer.Flush();
         fs.Close();
        }