Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多类XML序列化程序生成大部分空白XML文件_C#_Xml_Serialization - Fatal编程技术网

C# 多类XML序列化程序生成大部分空白XML文件

C# 多类XML序列化程序生成大部分空白XML文件,c#,xml,serialization,C#,Xml,Serialization,我正在尝试创建一个多类XML文件,以便更好地组织配置文件中的选项。原因是这个程序有几个不同的函数,我不希望所有的选项都在XML中拼凑在一起 下面是我的XML代码 [Serializable] public class Database { public string DatabaseAddress { get; set; } public int DatabasePort { get; set; } public string DatabaseName { get; se

我正在尝试创建一个多类XML文件,以便更好地组织配置文件中的选项。原因是这个程序有几个不同的函数,我不希望所有的选项都在XML中拼凑在一起

下面是我的XML代码

[Serializable]
public class Database
{
    public string DatabaseAddress { get; set; }
    public int DatabasePort { get; set; }
    public string DatabaseName { get; set; }
    public string DatabaseTableName { get; set; }
    public string DatabaseUsername { get; set; }
    public string DatabasePassword { get; set; }
}

[Serializable]
public class Tools
{
    public bool EnableStaffManager { get; set; }
    public string StaffManagerChatColor { get; set; }
    public bool EnableServerSecurity { get; set; }

}

[Serializable]
public class StaffManager
{
    public bool EnableDutyAnnouncer { get; set; }
    public bool RemoveAdminOnLogout { get; set; }
    public List<Mod> Mods { get; set; }
    public List<Admin> Admins { get; set; }
}

[Serializable]
public class Loadout
{
    public List<Item> Items { get; set; }

}

public class PToolsConfiguration : IRocketPluginConfiguration
{
    Database db = new Database();
    Tools t = new Tools();
    StaffManager sm = new StaffManager();
    Loadout l = new Loadout();

    public void LoadDefaults()
    {
        db.DatabaseAddress = "localhost";
        db.DatabasePort = 3306;
        db.DatabaseName = "unturned";
        db.DatabaseTableName = "PTools";
        db.DatabaseUsername = "unturned";
        db.DatabasePassword = "password";

        t.EnableStaffManager = true;
        t.StaffManagerChatColor = "red";
        t.EnableServerSecurity = true;

        sm.EnableDutyAnnouncer = true;
        sm.RemoveAdminOnLogout = true;

        l.Items = new List<Item>() {
                    new Item() {
                        Id = 203,
                        Amt = 1
                    },
                    new Item() {
                        Id = 1015,
                        Amt = 1
                    },
                    new Item() {
                        Id = 1016,
                        Amt = 1
                    },
                    new Item() {
                        Id = 1017,
                        Amt = 3
                    },
                    new Item() {
                        Id = 333,
                        Amt = 3
                    },
                    new Item() {
                        Id = 93,
                        Amt = 3
                    },
                    new Item() {
                        Id = 466,
                        Amt = 2
                    },
                    new Item() {
                        Id = 151,
                        Amt = 1
                    },
                    new Item() {
                        Id = 345,
                        Amt = 1
                    },
                    new Item() {
                        Id = 347,
                        Amt = 15
                    },
                };
        sm.Mods = new List<Mod>() {
                   new Mod() {
                       STEAM64 = "76555555555551633"
                    }
                };
        sm.Admins = new List<Admin>() {
                   new Admin() {
                       STEAM64 = "76555555555551633"
                    }
                };
    }
}
[可序列化]
公共类数据库
{
公共字符串数据库地址{get;set;}
公共int数据库端口{get;set;}
public string DatabaseName{get;set;}
公共字符串数据库表名{get;set;}
公共字符串数据库用户名{get;set;}
公共字符串数据库密码{get;set;}
}
[可序列化]
公共类工具
{
public bool EnableStaffManager{get;set;}
公共字符串StaffManagerChatColor{get;set;}
public bool EnableServerSecurity{get;set;}
}
[可序列化]
公营员工经理
{
public bool EnableDutyAnnouncer{get;set;}
public bool removeadminologout{get;set;}
公共列表Mods{get;set;}
公共列表管理员{get;set;}
}
[可序列化]
公共类装船
{
公共列表项{get;set;}
}
公共类PToolsConfiguration:IRocketPluginConfiguration
{
数据库db=新数据库();
工具t=新工具();
StaffManager sm=新的StaffManager();
装船l=新装船();
公共void LoadDefaults()
{
db.DatabaseAddress=“localhost”;
db.DatabasePort=3306;
db.DatabaseName=“未翻转”;
db.DatabaseTableName=“PTools”;
db.DatabaseUsername=“未翻转”;
db.DatabasePassword=“password”;
t、 EnableStaffManager=true;
t、 StaffManagerChatColor=“红色”;
t、 EnableServerSecurity=true;
sm.EnableDutyAnnouncer=true;
sm.removeadminologout=true;
l、 Items=新列表(){
新项目(){
Id=203,
金额=1
},
新项目(){
Id=1015,
金额=1
},
新项目(){
Id=1016,
金额=1
},
新项目(){
Id=1017,
金额=3
},
新项目(){
Id=333,
金额=3
},
新项目(){
Id=93,
金额=3
},
新项目(){
Id=466,
金额=2
},
新项目(){
Id=151,
金额=1
},
新项目(){
Id=345,
金额=1
},
新项目(){
Id=347,
金额=15
},
};
sm.Mods=新列表(){
新Mod(){
STEAM64=“76555551633”
}
};
sm.Admins=新列表(){
新管理员(){
STEAM64=“76555551633”
}
};
}
}
这是它生成的XML

<?xml version="1.0" encoding="utf-8"?>
<PToolsConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

所以我想弄清楚我做错了什么,它没有生成整个XML文件。。。谢谢


我想正式表扬jdweng,因为他的回答正是我想要的。下面是使用他的代码后的输出

<PToolsConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Database>
    <DatabaseAddress>localhost</DatabaseAddress>
    <DatabasePort>3306</DatabasePort>
    <DatabaseName>unturned</DatabaseName>
    <DatabaseTableName>PTools</DatabaseTableName>
    <DatabaseUserName>unturned</DatabaseUserName>
    <DatabasePassword>password</DatabasePassword>
  </Database>
  <Tools>
    <EnableStaffManager>true</EnableStaffManager>
    <StaffManagerChatColor>red</StaffManagerChatColor>
    <EnableServerSecurity>true</EnableServerSecurity>
  </Tools>
  <StaffManager>
    <EnableDutyAnnouncer>true</EnableDutyAnnouncer>
    <RemoveAdminOnLogout>true</RemoveAdminOnLogout>
    <Mods>76555555555551633</Mods>
    <Admins>76555555555551633</Admins>
  </StaffManager>
  <Loadout>
    <Items>
      <Id>203</Id>
      <Amt>1</Amt>
    </Items>
    <Items>
      <Id>1015</Id>
      <Amt>1</Amt>
    </Items>
    <Items>
      <Id>1016</Id>
      <Amt>1</Amt>
    </Items>
    <Items>
      <Id>1017</Id>
      <Amt>3</Amt>
    </Items>
    <Items>
      <Id>333</Id>
      <Amt>3</Amt>
    </Items>
    <Items>
      <Id>93</Id>
      <Amt>3</Amt>
    </Items>
    <Items>
      <Id>466</Id>
      <Amt>2</Amt>
    </Items>
    <Items>
      <Id>151</Id>
      <Amt>1</Amt>
    </Items>
    <Items>
      <Id>345</Id>
      <Amt>1</Amt>
    </Items>
    <Items>
      <Id>347</Id>
      <Amt>15</Amt>
    </Items>
  </Loadout>
</PToolsConfiguration>

本地服务器
3306
未用尽
托尔
未用尽
密码
真的
红色
真的
真的
真的
76555555555551633
76555555555551633
203
1.
1015
1.
1016
1.
1017
3.
333
3.
93
3.
466
2.
151
1.
345
1.
347
15

感谢大家的快速反馈。

看起来您正在序列化
PToolsConfiguration
类的一个实例,该类没有任何公共属性。因此,这正是生成的XML应该是什么样子。

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    [XmlRoot("PToolsConfiguration")]
    public class PToolsConfiguration 
    {
        [XmlElement("Database")]
        public Database db { get; set; }
        [XmlElement("Tools")]
        public Tools t { get; set; }
        [XmlElement("StaffManager")]
        public StaffManager sm { get; set; }
        [XmlElement("Loadout")]
        public Loadout l { get; set; }

        public PToolsConfiguration()
        {
            db = new Database();
            t = new Tools();
            sm = new StaffManager();
            l = new Loadout();
        }
        public void LoadDefaults()
        {
            db.DatabaseAddress = "localhost";
            db.DatabasePort = 3306;
            db.DatabaseName = "unturned";
            db.DatabaseTableName = "PTools";
            db.DatabaseUsername = "unturned";
            db.DatabasePassword = "password";

            t.EnableStaffManager = true;
            t.StaffManagerChatColor = "red";
            t.EnableServerSecurity = true;

            sm.EnableDutyAnnouncer = true;
            sm.RemoveAdminOnLogout = true;

            l.Items = new List<Item>() {
                    new Item() {
                        Id = 203,
                        Amt = 1
                    },
                    new Item() {
                        Id = 1015,
                        Amt = 1
                    },
                    new Item() {
                        Id = 1016,
                        Amt = 1
                    },
                    new Item() {
                        Id = 1017,
                        Amt = 3
                    },
                    new Item() {
                        Id = 333,
                        Amt = 3
                    },
                    new Item() {
                        Id = 93,
                        Amt = 3
                    },
                    new Item() {
                        Id = 466,
                        Amt = 2
                    },
                    new Item() {
                        Id = 151,
                        Amt = 1
                    },
                    new Item() {
                        Id = 345,
                        Amt = 1
                    },
                    new Item() {
                        Id = 347,
                        Amt = 15
                    },
                };
            sm.Mods = new List<string>() { "76555555555551633"};

            sm.Admins = new List<string>() {"76555555555551633"};

        }
    }

    [XmlRoot("Database")]
    public class Database
    {
        [XmlElement("DatabaseAddress")]
        public string DatabaseAddress { get; set; }
        [XmlElement("DatabasePort")]
        public int DatabasePort { get; set; }
        [XmlElement("DatabaseName")]
        public string DatabaseName { get; set; }
        [XmlElement("DatabaseTableName")]
        public string DatabaseTableName { get; set; }
        [XmlElement("DatabaseUserName")]
        public string DatabaseUsername { get; set; }
        [XmlElement("DatabasePassword")]
        public string DatabasePassword { get; set; }
    }

    [XmlRoot("Tools")]
    public class Tools
    {
        [XmlElement("EnableStaffManager")]
        public bool EnableStaffManager { get; set; }
        [XmlElement("StaffManagerChatColor")]
        public string StaffManagerChatColor { get; set; }
        [XmlElement("EnableServerSecurity")]
        public bool EnableServerSecurity { get; set; }

    }

    [XmlRoot("StaffManager")]
    public class StaffManager
    {
        [XmlElement("EnableDutyAnnouncer")]
        public bool EnableDutyAnnouncer { get; set; }
        [XmlElement("RemoveAdminOnLogout")]
        public bool RemoveAdminOnLogout { get; set; }
        [XmlElement("Mods")]
        public List<string> Mods { get; set; }
        [XmlElement("Admins")]
        public List<string> Admins { get; set; }
    }

    [XmlRoot("Loadout")]
    public class Loadout
    {
        [XmlElement("Items")]
        public List<Item> Items { get; set; }

    }
    [XmlRoot("Item")]
    public class Item
    {
        [XmlElement("Id")]
        public int Id { get; set; }
        [XmlElement("Amt")]
        public Single Amt { get; set; }

    }


}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Xml;
使用System.Xml.Serialization;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
}
}
[XmlRoot(“PToolsConfiguration”)]
公共类PToolsConfiguration
{
[XmlElement(“数据库”)]
公共数据库db{get;set;}
[XmlElement(“工具”)]
公共工具t{get;set;}
[XmlElement(“员工经理”)]
公共员工管理器sm{get;set;}
[XmlElement(“装船”)]
公共装船l{get;set;}
公共PToolsConfiguration()
{
db=新数据库();
t=新工具();
sm=新员工经理();
l=新装船();
}
公共void LoadDefaults()
{
db.DatabaseAddress=“localhost”;
db.DatabasePort=3306;
db.DatabaseName=“未翻转”;
db.DatabaseTableName=“PTools”;
db.DatabaseUsername=“未翻转”;
db.DatabasePassword=“password”;
t、 EnableStaffManager=true;
t、 StaffManagerChatColor=“红色”;
t、 EnableServerSecurity=true;
sm.EnableDutyAnnouncer=true;
sm.removeadminologout=true;
l、 Items=新列表(){
新项目(){
Id=203,
金额=1