.net服务器端解析xml,放到list中遍历,发送
- <list>
- <com.vin.sip.model.Token>
<did>1</did>
<token>adfdasfsaf</token>
</com.vin.sip.model.Token>
- <com.vin.sip.model.Token>
<did>2</did>
<token>fadsfdgfghghgjh</token>
</com.vin.sip.model.Token>
- <com.vin.sip.model.Token>
<did>3</did>
<token>hgfhfgjhgkjhk</token>
</com.vin.sip.model.Token>
</list>
[解决办法]
xml
- <list>
- <com.vin.sip.model.Token>
<did>1</did>
<token>adfdasfsaf</token>
</com.vin.sip.model.Token>
- <com.vin.sip.model.Token>
<did>2</did>
<token>fadsfdgfghghgjh</token>
</com.vin.sip.model.Token>
- <com.vin.sip.model.Token>
<did>3</did>
<token>hgfhfgjhgkjhk</token>
</com.vin.sip.model.Token>
</list>
第一步Model层
namespace WebService1
{
public class Model
{
public int did
{
get ; set;
}
public string token
{
get ; set ;
}
第二步读取xml累
namespace WebService1
{
public class Model
{
private int did;
private string token;
public int Did
{
get { return did; }
set { did = value; }
}
public string Token
{
get { return token; }
set { token = value; }
}
}
}
class ReadXml
{
public List<Model> Read(string path)
{
// string did = "",
string token = "";
//string Url = "http://192.168.10.80:8080/SIP/tokenAction.do?deviceToken=FA&type=get";
List<Model> list = new List<Model>();
XmlReaderSettings xmlSet = new XmlReaderSettings();
using (XmlReader reader = XmlReader.Create(path, xmlSet))
{
while (reader.Read())//使用XmlReader对象单向有效地读取xml文件
{
Model notification = new Model();
//if (reader.NodeType == XmlNodeType.Element)
//{
// did = reader.ReadString();
//}
if (reader.NodeType == XmlNodeType.Element && "token" == reader.LocalName)
{
token = reader.ReadString();
/tification.did = did;
notification.token = token;
list.Add(notification);
}
}
}
return list;
}
}
第三步调用和显示
claas program
{
static void Main (string[] args)
{
ReadXml read = new ReadXml();
Display(read.Read("http://192.168.10.80:8080/SIP/tokenAction.do?deviceToken=FA&type=get"));
}
private static void Display(List<Model> list)//遍历显示从xml读取的信息
{
foreach (Model notification in list)
{
Console.WriteLine(notification.did + "," + notification.token);
}
}
}
/// <summary>
/// C#讲简单xml文件读取到DataTable
/// </summary>
/// <param name="filePath">xml文件绝对路径</param>
/// <param name="TagName">要索索的子元素节点名称</param>
/// <param name="NodeAttributs">子元素</param>
/// <returns></returns>
protected DataTable ReadXmlToDataTable(string filePath, string TagName, string[] NodeAttributs)
{
DataTable dt = new DataTable();
for (int i = 0; i < NodeAttributs.Length; i++)
{
dt.Columns.Add(NodeAttributs[i], typeof(string));
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNodeList nodeList = xmlDoc.GetElementsByTagName(TagName);
foreach (XmlNode node in nodeList)
{
DataRow dr = dt.NewRow();
for (int i = 0; i < NodeAttributs.Length; i++)
{
dr[NodeAttributs[i]] = node.ChildNodes[i].InnerText;
}
dt.Rows.Add(dr);
}
return dt;
}