C# XML反序列化不需要';行不通 公共类PersistableObject { 公共静态T加载(字符串文件名),其中T:PersistableObject,new() { T结果=默认值(T); 使用(XmlReader=XmlReader.Create(文件名)) { 结果=新的XmlSerializer(typeof(T))。将(读取器)反序列化为T; } 返回结果; } public void Save(字符串文件名),其中T:PersistableObject { 使用(FileStream stream=newfilestream(fileName,FileMode.CreateNew)) { 新的XmlSerializer(typeof(T)).Serialize(stream,this); } } } 公共类数据库配置:PersistableObject { 公共字符串主机{get;set;} 公共字符串架构{get;set;} 公共字符串用户名{get;set;} 公共字符串密码{get;set;} }
我使用以下代码加载XML:C# XML反序列化不需要';行不通 公共类PersistableObject { 公共静态T加载(字符串文件名),其中T:PersistableObject,new() { T结果=默认值(T); 使用(XmlReader=XmlReader.Create(文件名)) { 结果=新的XmlSerializer(typeof(T))。将(读取器)反序列化为T; } 返回结果; } public void Save(字符串文件名),其中T:PersistableObject { 使用(FileStream stream=newfilestream(fileName,FileMode.CreateNew)) { 新的XmlSerializer(typeof(T)).Serialize(stream,this); } } } 公共类数据库配置:PersistableObject { 公共字符串主机{get;set;} 公共字符串架构{get;set;} 公共字符串用户名{get;set;} 公共字符串密码{get;set;} },c#,xml,C#,Xml,我使用以下代码加载XML: public class PersistableObject { public static T Load<T>(string fileName) where T : PersistableObject, new() { T result = default(T); using (XmlReader reader = XmlReader.Create(fileName
public class PersistableObject
{
public static T Load<T>(string fileName) where T : PersistableObject, new()
{
T result = default(T);
using (XmlReader reader = XmlReader.Create(fileName))
{
result = new XmlSerializer(typeof(T)).Deserialize(reader) as T;
}
return result;
}
public void Save<T>(string fileName) where T : PersistableObject
{
using (FileStream stream = new FileStream(fileName, FileMode.CreateNew))
{
new XmlSerializer(typeof(T)).Serialize(stream, this);
}
}
}
public class DatabaseConfiguration : PersistableObject
{
public string Host { get; set; }
public string Schema { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
var配置=PersistableObject.Load(“Database.xml”);
但是,配置的属性为空。这是Database.xml:
var configuration = PersistableObject.Load<DatabaseConfiguration>("Database.xml");
本地服务器
铬
根
由于某种原因,它们保持为空,并且没有分配任何内容。这是为什么?您的
Database.xml
内容不正确,特别是它的第二行关闭了DatabaseConfiguration
元素
替换为:
<?xml version="1.0"?>
<DatabaseConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
<Host>localhost</Host>
<Schema>chromium</Schema>
<Username>root</Username>
<Password></Password>
</DatabaseConfiguration>
本地服务器
铬
根
您的Database.xml
内容不正确,特别是它的第二行关闭了DatabaseConfiguration
元素
替换为:
<?xml version="1.0"?>
<DatabaseConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
<Host>localhost</Host>
<Schema>chromium</Schema>
<Username>root</Username>
<Password></Password>
</DatabaseConfiguration>
本地服务器
铬
根
您的Database.xml
内容不正确,特别是它的第二行关闭了DatabaseConfiguration
元素
替换为:
<?xml version="1.0"?>
<DatabaseConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
<Host>localhost</Host>
<Schema>chromium</Schema>
<Username>root</Username>
<Password></Password>
</DatabaseConfiguration>
本地服务器
铬
根
您的Database.xml
内容不正确,特别是它的第二行关闭了DatabaseConfiguration
元素
替换为:
<?xml version="1.0"?>
<DatabaseConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
<Host>localhost</Host>
<Schema>chromium</Schema>
<Username>root</Username>
<Password></Password>
</DatabaseConfiguration>
本地服务器
铬
根
请看,共识是“不,他们不应该”!请看,那里的共识是“不,他们不应该”!请看,那里的共识是“不,他们不应该”!请看,那里的共识是“不,他们不应该”!