XML模式指定文件索引。下面是XML文件的外观示例。
<?xml version="1.0" encoding="utf-8"?>
<fIndex xsi:schemaLocation="http:address fIndex.xsd" xmlns="address" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<f>
<foN>SomeDir</foN>
<fiN>file1.txt</fiN>
</f>
<f>
<foN>SomeDir</foN>
<fiN>file2.txt</fiN>
</f>
</fIndex>
我使用xsd来创建XML所需的类。然后填写一些示例代码以匹配上面的示例。
class Program
{
static void Main(string[] args)
{
fileIndexType table = new fileIndexType();
fileIndexTypeF element1 = new fileIndexTypeF();
fileIndexTypeF element2 = new fileIndexTypeF();
element1.fiN = @"C:\SomeDir";
element1.foN = @"file1.txt";
element2.fiN = @"C:\SomeDir";
element2.foN = @"file2.txt";
fileIndexTypeF[] files = new fileIndexTypeF[2] { element1, element2 };
table.f = files;
}
}
如何创建上述XML文件?
答案 0 :(得分:4)
您需要serialize个对象。
XmlSerializer
类可用于此:
XmlSerializer serializer = new XmlSerializer(typeof(fileIndexType));
using(Stream writer = new FileStream(filename, FileMode.Create))
{
serializer.Serialize(writer, table );
writer.Close();
}