我第一次使用XML和配置文件。 我正在尝试读取此配置文件以连接到不同的连接字符串。
<?xml version="1.0"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<BDD id="aaa">
<Connexion>
<MetaDatas>
<Data id="SOC">Model</Data>
</MetaDatas>
<Provider>
<Name>System.Data.SqlClient</Name>
<DataSource>10.198.164.169</DataSource>
<Catalog>xxx</Catalog>
<User>sa</User>
<Password>xxx</Password>
</Provider>
</Connexion>
</BDD>
<BDD id="bbb">
<Connexion>
<MetaDatas>
<Data id= "SE">eee</Data>
<Data id="AD">fff</Data>
<Data id="FR">ggg</Data>
<Data id="DOC">hhh</Data>
</MetaDatas>
<Provider>
<Name>System.Data.SqlClient</Name>
<DataSource>10.198.164.169</DataSource>
<Catalog>yyy</Catalog>
<User>sa</User>
<Password>xx</Password>
</Provider>
</Connexion>
</BDD>
</Config>
使用以下代码结构,
[XmlRoot("Config")]
public class XmlConfigBDD : XmlFileConfiguration.IXmlConfiguration
{
[XmlArray("Config")]
[XmlArrayItem(typeof(DataBase), ElementName="BDD")]
public List<DataBase> BDD { get; set; }
public XmlFileConfiguration.IXmlConfiguration Default
{
get { return new XmlConfigBDD(); }
}
public XmlConfigBDD()
{
this.BDD = new List<DataBase>();
}
public string Path
{
get { return SE.Datas.Common.AppPaths.GetBddConfigurationFilePath(); }
}
}
public class DataBase
{
[XmlAttribute("id")]
public string Id { get; set; }
[XmlElement("Connexion")]
public ConfigConnection Connexion { get; set; }
public DataBase()
{
this.Connexion = new ConfigConnection();
}
}
public class ConfigConnection
{
[XmlArray("MetaDatas")]
//public ConfigMetaDatas MetaDatas { get; set; }
public List<ConfigMetaDatas> MetaDatas { get; set; }
[XmlElement("Provider")]
public ConfigProvider Provider { get; set; }
protected static readonly string _Connection_ = @"metadata=res://*/%Metadata%.csdl|res://*/%Metadata%.ssdl|res://*/%Metadata%.msl;provider=%Provider%;provider connection string=""%ProviderConnectionString%""";
public ConfigConnection()
{
this.MetaDatas = new List<ConfigMetaDatas>();
this.Provider = new ConfigProvider();
}
protected static readonly string _Metadata_TobeReplace_ = "%Metadata%";
protected static readonly string _Provider_TobeReplace_ = "%Provider%";
protected static readonly string _ProviderString_TobeReplace_ = "%ProviderConnectionString%";
public string getModelConnection()
{
return this.getConnectionString(this.MetaDatas.Find(ctx => ctx.Id == "SOC").Value);
}
/*...*/
}
public class ConfigMetaDatas
{
[XmlAttribute("id")]
public string Id { get; set; }
[XmlText(Type = typeof(string))]
public string Value { get; set; }
public ConfigMetaDatas()
{
}
}
public class ConfigProvider
{
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("DataSource")]
public string DataSource { get; set; }
[XmlElement("Catalog")]
public string Catalog { get; set; }
[XmlElement("User", IsNullable = true)]
public string User { get; set; }
[XmlElement("Password", IsNullable = true)]
public string Password { get; set; }
public ConfigProvider()
{
}
/*...*/
}
它没有用,我尝试了不同的组合。 返回的连接字符串为null。
答案 0 :(得分:1)
替换:
[XmlArray("Config")]
[XmlArrayItem(typeof(DataBase), ElementName="BDD")]
public List<DataBase> BDD { get; set; }
使用:
[XmlElement]
public List<DataBase> BDD { get; set; }
(原始版本表明根<Config>
内应该有一个额外的<Config>
而因为元数据 具有父/子xml结构,它应该是:
[XmlArray("MetaDatas")]
[XmlArrayItem("Data")]
public List<ConfigMetaDatas> MetaDatas { get; set; }