从哈希表中编写XML转换为表对象,然后加载到数据集

时间:2013-07-05 20:43:19

标签: c# xml

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <blog>
    <Title xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Testing this XML File</Title>
    <Name xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Shawn</Name>
    <Image xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Hosting\html\blogimage\</image>
    <Comment xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Shawn is testing this file</Comment>
  </blog>
</NewDataSet>

当我使用数据集Writexml方法时,我一直得到这个xml文件类型,我希望我的XML文件看起来像这样:

<NewDataSet>
  <blog>
    <Title>Testing this XML File</Title>
    <Name>Shawn</Name>
    <Image>Hosting\html\blogimage\</image>
    <Comment>Shawn is testing this file</Comment>
  </blog>
</NewDataSet>

此xml适用于包含图像文件路径的博客,因此当博客归档时,它将加载图像 我想知道,当我想调用xml标签来读取xml文件时,标签中的第一个xml会受到影响,标签中的w3c链接需要理解为什么他们从数据集中这样写它从未这样做过之前。

这是编写xml之前的代码:Sample

public void WriteDataXml(string name, string title, string comment, string path)
    {

      DataSet ds = new DataSet();

     //Sets the data in the hashtable to write new file
     Hashtable lines = new Hashtable();
     lines["Name"] = name;
     lines["Title"] = title;
     lines["Comment"] = comment;
     lines["image"] = path;

     //Convert hash table to data table
     //create an instance of DataTable
     var dataTable = new DataTable(lines.GetType().Name);
     //specify the table name       
     dataTable.TableName = "blog";
     //fill the columns in the DataTable
     foreach (DictionaryEntry entry in lines)
     {
         dataTable.Columns.Add(entry.Key.ToString(), typeof(object));
     }

     //create a new DataRow in the DataTable    
     DataRow dr = dataTable.NewRow();
     //fill the new row in the DataTable
     foreach (DictionaryEntry entry in lines)
     {
         dr[entry.Key.ToString()] = entry.Value.ToString();
     }
     //add the filled up row to the DataTable
     dataTable.Rows.Add(dr);

    //Appending To Existing: Pass values to the dataset to write the xml file from the hash table

     // Add Table to dataset ds
     ds.Tables.Add(dataTable);

    //Reading Existing: Also include current files from the xml in the data set by reading and current

    //Write XML
     string filename = Server.MapPath(@".\blog\") + "comments.xml";
     ds.WriteXml(filename);

    }

1 个答案:

答案 0 :(得分:0)

您可以使用Linq to Xml创建xml而不是所有代码:

public static void WriteDataXml(
    string name, string title, string comment, string path)
{
    XDocument xdoc = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XElement("Blog",
            new XElement("Title", title),
            new XElement("Name", name),
            new XElement("Image", path),
            new XElement("Comment", comment)));

    string filename = Server.MapPath(@".\blog\") + "comments.xml";
    xdoc.Save(filename);
}

输出(我认为这里不需要NewDataSet元素):

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Blog>
  <Title>Testing this XML File</Title>
  <Name>Bob</Name>
  <Image>Hosting\html\blogimage\</Image>
  <Comment>Shawn is testing this file</Comment>
</Blog>