c# - Xml 和数据集的问题

标签 c# xml

这个问题很可能非常简单,但我正在制作一个数据集并尝试使用 XMLTextWriter 将其保存到文件

现在,当我保存数据集然后尝试读取它时,它会读取表名称,但将行显示为 0?所以看来我的作者没有写表格的行?

有人知道我该如何解决这个问题吗?

public static DataSet liveData = new DataSet();

    public static DataTable players = new DataTable("Players");

public static void WriteSchemaWithXmlTextWriter()
    {
        // Set the file path and name. Modify this for your purposes. 
        string filename = "Schema.xml";

        // Create a FileStream object with the file path and name.
        System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Create);

        // Create a new XmlTextWriter object with the FileStream.
        System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.Unicode);

        // Write the schema into the DataSet and close the reader.
        liveData.WriteXmlSchema(writer);
        writer.Close();
    }

    public static void ReadSchemaFromXmlTextReader()
    {
        // Set the file path and name. Modify this for your purposes. 
        string filename = "Schema.xml";

        // Create a FileStream object with the file path and name.
        System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open);

        // Create a new XmlTextReader object with the FileStream.
        System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stream);

        // Read the schema into the DataSet and close the reader.
        liveData.ReadXmlSchema(xmlReader);
        Console.WriteLine("y: {0}", liveData.Tables["Players"].Rows.Count);
        xmlReader.Close();
    }

感谢您提前提供的帮助:)

最佳答案

您正在编写架构,即 Xml 的布局/结构,而不是实际内容。

您需要使用DataSet.WriteXml ...

https://msdn.microsoft.com/en-us/library/ms135426%28v=vs.110%29.aspx

...相反!

关于c# - Xml 和数据集的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31526588/

相关文章:

c# - 为什么在 NServiceBus.Host.exe 中托管 NserviceBus 端点的 Windows 服务在没有为 exe 报告错误时无法启动?

java - 当标签名称未知时解析 XML

c# - 何时使用 TaskEx.Run 与 TaskEx.RunEx

c# - 需要比较单独的 foreach 循环产生的值

.net - 从 Controller 返回 XDocument (dotnet coreclr)

xml - 根据 XSL 中的条件动态输出元素?

python - 如何在 Haskell 中重新实现这个 Python XML 解析函数?

XML:用作列表分隔符的内容

c# - 如何将 MAC 地址从字符串转换为无符号整数

c# - ServiceStack MsgPackServiceClient 在获取数据时失败但 JsonServiceClient 工作