c#读取xml的内容并放到对象中
<?xml version="1.0" encoding="UTF-8"?> <serviceResponse xmlns:cas="http://www.yale.edu/tp/cas"> <authenticationSuccess> <userId>1547328470384640</userId> <user>admin</user> <domain>testjn14</domain> <email/> <loginName>admin</loginName> </authenticationSuccess> </serviceResponse>
一、c#最原始的方法:
//定义xml对应的类
public class CasMolde { public string userId { get; set; } public string user { get; set; } public string domain { get; set; } public string email { get; set; } public string loginName { get; set; } }
//读取xml并实例化对象 (最原始的方法)
private CasMolde GetCasModel(string xml) { CasMolde casmodel = new CasMolde(); XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); foreach (XmlNode item in doc.DocumentElement.ChildNodes) { foreach (XmlNode item2 in item.ChildNodes) { switch (item2.Name) { case "userId": casmodel.userId = item2.InnerXml; break; case "user": casmodel.user = item2.InnerXml; break; case "loginName": casmodel.loginName = item2.InnerXml; break; case "email": casmodel.email = item2.InnerXml; break; case "domain": casmodel.domain = item2.InnerXml; break; default: break; } } } return casmodel; }