0
<?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>

当我使用 Dataset 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之前的代码:示例

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

    }
4

1 回答 1

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>
于 2013-07-05T21:15:54.357 回答