DotNet6中的XMLSerializer以及Dotnet6中的XMLSerializer不编写Whiteves?

发布于 2025-01-27 14:49:39 字数 993 浏览 1 评论 0原文

实际上,我在C#中尝试了此基本代码,该代码将数据集序列化为XML文档。

static void Main(string[] args)
{
    XmlSerializer ser = new XmlSerializer(typeof(DataSet));

    // Creates a DataSet; adds a table, column, and ten rows.
    DataSet ds = new DataSet("myDataSet");
    DataTable t = new DataTable("table1");
    DataColumn c = new DataColumn("thing");
    t.Columns.Add(c);
    ds.Tables.Add(t);
    DataRow r;
    for (int i = 0; i < 10; i++)
    {
        r = t.NewRow();
        r[0] = "Thing " + i;
        t.Rows.Add(r);
    }
    StreamWriter writer = new StreamWriter("DemoNet5.xml");
    ser.Serialize(writer, ds);
    writer.Close();
}

这将创建使用适当的空格和.NET 5中的“ Demonet5.xml”文件。但是,当我尝试在.NET 6中运行相同的代码时 我没有得到白色的和凹痕,因此XML的内容是单行编写的。

稍后,当我尝试阅读该.xml文档时,我会面临问题。 有人可以帮助我吗?我希望我的XML内容具有适当的格式,该格式与适当的不同和空格相关。 使用.net6 创建的XML文件 我还使用了使用dotnet6创建的XML文档。

Actually I tried this basic code in C# which serializes the DataSet into XML document.

static void Main(string[] args)
{
    XmlSerializer ser = new XmlSerializer(typeof(DataSet));

    // Creates a DataSet; adds a table, column, and ten rows.
    DataSet ds = new DataSet("myDataSet");
    DataTable t = new DataTable("table1");
    DataColumn c = new DataColumn("thing");
    t.Columns.Add(c);
    ds.Tables.Add(t);
    DataRow r;
    for (int i = 0; i < 10; i++)
    {
        r = t.NewRow();
        r[0] = "Thing " + i;
        t.Rows.Add(r);
    }
    StreamWriter writer = new StreamWriter("DemoNet5.xml");
    ser.Serialize(writer, ds);
    writer.Close();
}

And this creates the "DemoNet5.xml" file with proper WhiteSpaces and Indentation in .NET 5. But When I try to run the same code in .NET 6
I am not getting the WhiteSapces and Indentation so that the contents of the xml are written in single line.

Later on when I try to read that .xml document I face issues.
Is there someone who can help me with this ? I want my xml contents to be in proper format that is with proper indetation and WhiteSpaces.
Xml file created using .NET6
I have also attached the XML document created using DotNet6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文