SQL查询 生成XML

1

select t.UserNameXML.value('(/b/UserName)[1]','varchar(20)') as Name /*查询XML中的UserName值*/
from
(select a.* ,(select b.UserName from dbo.T_UserLogin b where b.LoginId=a.LoginId  for xml auto, type, elements) as UserNameXML
 from T_UserInfo a) t 

查询结果:




其中以下这条语句查询结果是:

select a.* ,(select b.UserName from dbo.T_UserLogin b where b.LoginId=a.LoginId  for xml auto, type, elements) as UserNameXML
 from T_UserInfo a
查询结果:




将XML序列化成一个Liset对象,和一个对象

protected List<T> XmlListInit<T>(string value) where T : BaseTO, new()
    {
    var objs = new List<T>();
    XmlDocument doc = new XmlDocument();
    XmlElement root = doc.CreateElement("root");
    doc.AppendChild(root);
    root.InnerXml = value;
    foreach (XmlNode node in root.ChildNodes)
    {
        var o = new T();
        o.Initialize(node);
        objs.Add(o);
    }
    return objs;
}

protected T XmlInit<T>(string value) where T : BaseTO, new()
{
    var obj = new T();
    XmlDocument doc = new XmlDocument();
    XmlElement root = doc.CreateElement("root");
    doc.AppendChild(root);
    root.InnerXml = value;
    if (value != null)
    {
        obj.Initialize(root.FirstChild);
    }
    return obj;
}




已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页