首先是json与实体类之间的的转换,现有如下json数据:
{
"Content": "中午12点开会",
"ResultCode": "502",
"Sign": "签名",
"Names": ["赵小美", "莉莎", "林妹妹"]
}
根据json数据定义实体类:
public class JsonRoot
{
public string Content { get; set; }
public string ResultCode { get; set; }
public string Sign { get; set; }
public List<string> Names { get; set; }
}
转换操作:
public JsonRoot JsonToEntityClass()
{
string js = "{\"Content\":\"中午12点开会\",\"ResultCode\":\"502\",\"Sign\":\"签名\",\"Names\":[\"赵小美\",\"莉莎\",\"林妹妹\"]}";
return JsonConvert.DeserializeObject<JsonRoot>(js);
}
同样将实体类转换为json如下:
public string EntityClassToJson()
{
JsonRoot jr = new JsonRoot()
{
Content = "中午12点开会",
ResultCode = "502",
Sign = "签名",
Names = new List<string>() {"赵小美","莉莎","林妹妹" }
};
return JsonConvert.SerializeObject(jr);
}
下面是xml与实体类的转换,有如下xml:
<ROOT>
<SCHOOL_ID>S00001</SCHOOL_ID>
<SCHOOL_NAME>清华大学</SCHOOL_NAME>
<GRADE>
<GRADE_NAME>1年级</GRADE_NAME>
<GRADE_ID>2008</GRADE_ID>
<STUDENT_NUMBER>0</STUDENT_NUMBER>
<MAJOR>
<MAJOR_NAME>软件工程</MAJOR_NAME>
<MAJOR_ID>1003</MAJOR_ID>
</MAJOR>
<MAJOR>
<MAJOR_NAME>软件工程</MAJOR_NAME>
<MAJOR_ID>2003</MAJOR_ID>
</MAJOR>
</GRADE>
<GRADE>
<GRADE_NAME>2年级</GRADE_NAME>
<GRADE_ID>3008</GRADE_ID>
<STUDENT_NUMBER>200</STUDENT_NUMBER>
<MAJOR>
<MAJOR_NAME>软件工程</MAJOR_NAME>
<MAJOR_ID>1003</MAJOR_ID>
</MAJOR>
<MAJOR>
<MAJOR_NAME>软件工程</MAJOR_NAME>
<MAJOR_ID>2003</MAJOR_ID>
</MAJOR>
</GRADE>
</ROOT>
首先是xml转实体类,实体类定义如下:
[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
[System.Xml.Serialization.XmlRoot(Namespace = "", IsNullable = false)]
public partial class ROOT
{
public string SCHOOL_ID { get; set; }
public string SCHOOL_NAME { get; set; }
[System.Xml.Serialization.XmlElement("GRADE")]
public List<GRADE> GRADE { get; set; }
}
[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class GRADE
{
public string GRADE_NAME { get; set; }
public string GRADE_ID { get; set; }
public string STUDENT_NUMBER { get; set; }
[System.Xml.Serialization.XmlElement("MAJOR")]
public List<MAJOR> MAJOR { get; set; }
}
[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[System.Xml.Serialization.XmlType(AnonymousType = true)]
public partial class MAJOR
{
public string MAJOR_NAME { get; set; }
public string MAJOR_ID { get; set; }
}
xml转实体类如下操作:
public ROOT XMlToEntityClass()
{
ROOT ec = new ROOT();
string xml = "<ROOT><SCHOOL_ID>S00001</SCHOOL_ID><SCHOOL_NAME>清华大学</SCHOOL_NAME><GRADE><GRADE_NAME>1年级</GRADE_NAME><GRADE_ID>2008</GRADE_ID><STUDENT_NUMBER>0</STUDENT_NUMBER><MAJOR><MAJOR_NAME>软件工程</MAJOR_NAME><MAJOR_ID>1003</MAJOR_ID></MAJOR><MAJOR><MAJOR_NAME>软件工程</MAJOR_NAME><MAJOR_ID>2003</MAJOR_ID></MAJOR></GRADE><GRADE><GRADE_NAME>2年级</GRADE_NAME><GRADE_ID>3008</GRADE_ID><STUDENT_NUMBER>200</STUDENT_NUMBER><MAJOR><MAJOR_NAME>软件工程</MAJOR_NAME><MAJOR_ID>1003</MAJOR_ID></MAJOR><MAJOR><MAJOR_NAME>软件工程</MAJOR_NAME><MAJOR_ID>2003</MAJOR_ID></MAJOR></GRADE></ROOT>";
using (StringReader sr = new StringReader(xml))
{
XmlSerializer serializer = new XmlSerializer(typeof(ROOT));
ec = serializer.Deserialize(sr) as ROOT;
}
return ec;
}
同样由实体类生成xml:
public string EntityClassToXml()
{
List<GRADE> lsMst = new List<GRADE>();
for (int i = 0; i < 2; i++)
{
List<MAJOR> lsDtl = new List<MAJOR>();
for (int j = 0; j < 2; j++)
{
MAJOR dtl = new MAJOR
{
MAJOR_NAME = "软件工程",
MAJOR_ID = j+1+"003"
};
lsDtl.Add(dtl);
}
GRADE mst = new GRADE
{
GRADE_NAME = i + 1 + "年级",
GRADE_ID = i + 2 + "008",
STUDENT_NUMBER = (i * 200).ToString(),
MAJOR = lsDtl
};
lsMst.Add(mst);
}
ROOT root = new ROOT
{
SCHOOL_ID = "S00001",
SCHOOL_NAME = "清华大学",
GRADE = lsMst
};
string s1 = string.Empty;
using (StringWriter sw = new StringWriter())
{
XmlSerializer serializer = new XmlSerializer(root.GetType());
serializer.Serialize(sw, root);
sw.Close();
s1 = sw.ToString();
}
XmlDocument doc = new XmlDocument();
doc.LoadXml(s1);
//移除首节点
XmlNode xmlNode = doc.FirstChild;
doc.RemoveChild(xmlNode);
//移除ROOT属性
XmlElement node = (XmlElement)doc.SelectSingleNode("ROOT");
node.RemoveAllAttributes();
s1 = doc.InnerXml;
return s1;
}
最后贴一个vs的使用技巧: