日期:2014-05-20  浏览次数:20916 次

linq to xml获取数据构造Model
XML code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!--测试环境数据-->
    <add key="Appkey" value="12337722" />
    <add key="Appsecret" value="sandboxda3e9856fbb65050b45a5efef" />
    <add key="Url" value="http://gw.api.tbsandbox.com/router/rest" />
    <add key="SessionKey" value="" />
    <!--正式环境数据-->
    <add key="OnLineAppkey" value="12337722" />
    <add key="OnLineAppsecret" value="0ed1394da3e9856fbb65050b45a5efef" />
    <add key="OnLineUrl" value="http://gw.api.taobao.com/router/rest" />
    <add key="OnLineSessionKey" value="" />
  </appSettings>
</configuration>

上面是我的xml格式
C# code
 public class ConfigModel
    {
        public string TandBoxAppkey { get; set; }
        public string TandBoxAppsecret { get; set; }
        public string TandBoxUrl{get;set;}
        public string TandBoxSessionKey { get; set; }
        public string OnLineAppkey { get; set; }
        public string OnLineAppsecret { get; set; }
        public string OnLineUrl { get; set; }
        public string OnLineSessionKey { get; set; }
    }

上面是我定义的实体 
我想通过linq to xml读取内容然后构造实体model返回
求指点

------解决方案--------------------
思路 你的 去循环就行了 不过我发现 如果你是读 config文件的话 没必要这么做 用.net 提供的类库 configmanager.appsettings 之类就行了.
XDocument config = XDocument.Load(Server.MapPath("Web.config"));
var tt = from c in config.Descendants("appSettings").Elements("add")
select new
{
key = c.Attribute("key").Value,

value = c.Attribute("value").Value,

};
------解决方案--------------------
同意一楼 这个真没必要用LINQ2XML
不过非要用的话 也是可以的

C# code


void Main()
{
    string xml=@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<configuration>
  <appSettings>
    <!--测试环境数据-->
    <add key=""Appkey"" value=""12337722"" />
    <add key=""Appsecret"" value=""sandboxda3e9856fbb65050b45a5efef"" />
    <add key=""Url"" value=""http://gw.api.tbsandbox.com/router/rest"" />
    <add key=""SessionKey"" value="""" />
    <!--正式环境数据-->
    <add key=""OnLineAppkey"" value=""12337722"" />
    <add key=""OnLineAppsecret"" value=""0ed1394da3e9856fbb65050b45a5efef"" />
    <add key=""OnLineUrl"" value=""http://gw.api.taobao.com/router/rest"" />
    <add key=""OnLineSessionKey"" value="""" />
  </appSettings>
</configuration>";
    
   var data=XElement.Parse(xml).Descendants("add");
    ConfigModel cg=new ConfigModel
    {
     TandBoxAppkey=data.FirstOrDefault(d=>d.Attribute("key").Value=="Appkey").Attribute("value").Value,
     TandBoxAppsecret=data.FirstOrDefault(d=>d.Attribute("key").Value=="Appsecret").Attribute("value").Value,
     TandBoxUrl=data.FirstOrDefault(d=>d.Attribute("key").Value=="Url").Attribute("value").Value,
     TandBoxSessionKey=data.FirstOrDefault(d=>d.Attribute("key").Value=="SessionKey").Attribute("value").Value,
     OnLineAppkey=data.FirstOrDefault(d=>d.Attribute("key").Value=="OnLineAppkey").Attribute("value").Value,
     OnLineAppsecret=data.FirstOrDefault(d=>d.Attribute("key").Value=="OnLineAppsecret").Attribute("value").Value,
     OnLineUrl=data.FirstOrDefault(d=>d.Attribute("key").Value=="OnLineUrl").Attribute("value").Value,
     OnLineSessionKey=data.FirstOrDefault(d=>d.Attribute("key").Value=="OnLineSessionKey").Attribute("value").Value
    };
    Console.WriteLine(cg);
}
 public class ConfigModel
    {
        public string TandBoxAppkey { get; set; }
        public string TandBoxAppsecret { get; set; }
        public string TandBoxUrl{get;set;}
        public string TandBoxSessionKey { get; set; }
        public string OnLineAppkey { get; set; }
        public string OnLineAppsecret { get; set; }
        public string OnLineUrl { get; set; }
        public string OnLineSessionKey { get; set; }
    }