2 回答
TA贡献1851条经验 获得超3个赞
请尝试这个
XmlNode secondNode = getSecondNode();// read the second node from the res
if (secondNode != null)
{
XmlNodeList xnList = secondNode.SelectNodes("//NewDataSet//Departments");
foreach (XmlNode xn in xnList)
{
string code = xn.SelectSingleNode("Code") != null ? xn.SelectSingleNode("Code").Value : "";
string Description = xn.SelectSingleNode("Description") != null ? xn.SelectSingleNode("Description").Value : "";
}
}
else
{
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "http://www.w3.org/2001/XMLSchema");
XmlNode firstNode = getFirstNode();// read the first node from the res
XmlNodeList xnList = firstNode.SelectNodes("//bk:sequence//bk:element", nsmgr);
foreach (XmlNode xn in xnList)
{
string value = ((System.Xml.XmlElement)xn).Attributes["name"].Value;
}
}
TA贡献1842条经验 获得超12个赞
尝试以下 xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication122
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XNamespace ns = root.GetDefaultNamespace();
XNamespace nsDiffgr = root.GetNamespaceOfPrefix("diffgr");
XNamespace nsData = root.GetNamespaceOfPrefix("msdata");
List<Department> deparments = doc.Descendants(ns + "Departments").Select(x => new Department()
{
id = (string)x.Attribute(nsDiffgr + "id"),
code = (string)x.Element(ns + "Code"),
description = (string)x.Element(ns + "Description"),
rowOrder = (int)x.Attribute(nsData + "rowOrder")
}).ToList();
Dictionary<string, Department> dict = deparments
.GroupBy(x => x.id, y => y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
public class Department
{
public string id { get; set; }
public string code { get; set; }
public string description { get; set; }
public int rowOrder { get;set;}
}
}
- 2 回答
- 0 关注
- 120 浏览
添加回答
举报