当前位置:编程学习 > C#/ASP.NET >>

xml转实体类

xml 如下  
<content>
  <head>
    <callSystem>1111</callSystem>
</head>
<body>
     <paramInfo>
       <paramCode>Id</paramCode>
       <paramValue>
    <value>1</value>
       </paramValue >
     </paramInfo>
     <paramInfo>
       <paramCode>sex</paramCode>
       <paramValue>
    <value>2</value>
       </paramValue >
     </paramInfo>
</body>
</content>

实体类
public class AA
{
    public string Id{get;set;}
    public string sex{get;set;}
}

要怎么写才能把xml的值转成实体类 --------------------编程问答-------------------- 反序列化  序列化   --------------------编程问答-------------------- 补充一点xml是Webservice的形式传过来的 --------------------编程问答-------------------- 推荐看看linq to xml, 很好用。语句简单那。 --------------------编程问答-------------------- 求个大概的例子. --------------------编程问答--------------------

            XElement root = XElement.Load(xml);
AA aa = new AA();
            IEnumerable<XElement> result = from el in root.Elements("body").Elements("paramInfo") select el;
           

            foreach (XElement xe in result)
            {
if(xe.Element("paramCode").Value == "Id")
{
aa.Id =  xe.Element("paramValue").Value
}
else if(xe.Element("paramCode").Value == "sex")
{
aa.sex =  xe.Element("paramValue").Value
}
            }
--------------------编程问答-------------------- 要是用 XMLDocument 怎么读出来? --------------------编程问答--------------------
引用 5 楼 uinatlex 的回复:

            XElement root = XElement.Load(xml);
AA aa = new AA();
            IEnumerable<XElement> result = from el in root.Elements("body").Elements("paramInfo") select el;
           

            foreach (XElement xe in result)
            {
if(xe.Element("paramCode").Value == "Id")
{
aa.Id =  xe.Element("paramValue").Value
}
else if(xe.Element("paramCode").Value == "sex")
{
aa.sex =  xe.Element("paramValue").Value
}
            }
  XElement这个有Load? --------------------编程问答-------------------- 求大神用XMLDocument 给个例子看一下. --------------------编程问答--------------------
引用 7 楼 zahir2011 的回复:
Quote: 引用 5 楼 uinatlex 的回复:


            XElement root = XElement.Load(xml);
AA aa = new AA();
            IEnumerable<XElement> result = from el in root.Elements("body").Elements("paramInfo") select el;
           

            foreach (XElement xe in result)
            {
if(xe.Element("paramCode").Value == "Id")
{
aa.Id =  xe.Element("paramValue").Value
}
else if(xe.Element("paramCode").Value == "sex")
{
aa.sex =  xe.Element("paramValue").Value
}
            }
  XElement这个有Load?


没得我骗你的 --------------------编程问答-------------------- 可以直接序列化成实体,但是实体和你的XML 结构要一样,

还有一种方式应该可行 用dynamic   --------------------编程问答--------------------
引用 9 楼 uinatlex 的回复:
Quote: 引用 7 楼 zahir2011 的回复:

Quote: 引用 5 楼 uinatlex 的回复:


            XElement root = XElement.Load(xml);
AA aa = new AA();
            IEnumerable<XElement> result = from el in root.Elements("body").Elements("paramInfo") select el;
           

            foreach (XElement xe in result)
            {
if(xe.Element("paramCode").Value == "Id")
{
aa.Id =  xe.Element("paramValue").Value
}
else if(xe.Element("paramCode").Value == "sex")
{
aa.sex =  xe.Element("paramValue").Value
}
            }
  XElement这个有Load?


没得我骗你的
  大神不要忽悠我... --------------------编程问答--------------------
引用 10 楼 Net_Java_dram 的回复:
可以直接序列化成实体,但是实体和你的XML 结构要一样,

还有一种方式应该可行 用dynamic  
不用序列化  我的xml是以支付串的形式传过来,xmldocumnet要怎么用? --------------------编程问答-------------------- 是不是我的问题有歧义? 在补充一点 paramCode 对应实体类  然后value是要给对应实体类赋的值!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,