<?xml version="1.0" standalone="yes"?>
<users>
<xs:schema id="users" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="users" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="user"><!--这里是表名-->
<xs:complexType>
<xs:sequence>
<xs:element name="userName" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
<xs:element name="userPass" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
</xs:sequence>
<xs:attribute name="id" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
</users>
比如上面这个怎么获取<xs:element name="user"> 下的三个name值 ID UserName UserPass到一个数组
<users>
<xs:schema id="users" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="users" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="user"><!--这里是表名-->
<xs:complexType>
<xs:sequence>
<xs:element name="userName" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
<xs:element name="userPass" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
</xs:sequence>
<xs:attribute name="id" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
</users>
比如上面这个怎么获取<xs:element name="user"> 下的三个name值 ID UserName UserPass到一个数组
#region 读取数据
/// <summary>
/// 读取指定节点的数据
/// </summary>
/// <param name="node">节点</param>
/// 使用示列:
/// XMLProsess.Read("/Node", "")
/// XMLProsess.Read("/Node/Element[@Attribute='Name']")
public string Read(string node)
{
string value = "";
try
{
XmlDocument doc = XMLLoad();
XmlNode xn = doc.SelectSingleNode(node);
value = xn.InnerText;
}
catch { }
return value;
} /// <summary>
/// 读取指定路径和节点的串联值
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// 使用示列:
/// XMLProsess.Read(path, "/Node", "")
/// XMLProsess.Read(path, "/Node/Element[@Attribute='Name']")
public static string Read(string path, string node)
{
string value = "";
try
{
XmlDocument doc = XMLLoad(path);
XmlNode xn = doc.SelectSingleNode(node);
value = xn.InnerText;
}
catch { }
return value;
} /// <summary>
/// 读取指定路径和节点的属性值
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// 使用示列:
/// XMLProsess.Read(path, "/Node", "")
/// XMLProsess.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
public static string Read(string path, string node, string attribute)
{
string value = "";
try
{
XmlDocument doc = XMLLoad(path);
XmlNode xn = doc.SelectSingleNode(node);
value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
}
catch { }
return value;
} /// <summary>
/// 获取某一节点的所有孩子节点的值
/// </summary>
/// <param name="node">要查询的节点</param>
public string[] ReadAllChildallValue(string node)
{
int i = 0;
string[] str = { };
XmlDocument doc = XMLLoad();
XmlNode xn = doc.SelectSingleNode(node);
XmlNodeList nodelist = xn.ChildNodes; //得到该节点的子节点
if (nodelist.Count > 0)
{
str = new string[nodelist.Count];
foreach (XmlElement el in nodelist)//读元素值
{
str[i] = el.Value;
i++;
}
}
return str;
} /// <summary>
/// 获取某一节点的所有孩子节点的值
/// </summary>
/// <param name="node">要查询的节点</param>
public XmlNodeList ReadAllChild(string node)
{
XmlDocument doc = XMLLoad();
XmlNode xn = doc.SelectSingleNode(node);
XmlNodeList nodelist = xn.ChildNodes; //得到该节点的子节点
return nodelist;
} /// <summary>
/// 读取XML返回经排序或筛选后的DataView
/// </summary>
/// <param name="strWhere">筛选条件,如:"name='kgdiwss'"</param>
/// <param name="strSort"> 排序条件,如:"Id desc"</param>
public DataView GetDataViewByXml(string strWhere, string strSort)
{
try
{
string XMLFile = this.XMLPath;
string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile;
DataSet ds = new DataSet();
ds.ReadXml(filename);
DataView dv = new DataView(ds.Tables[0]); //创建DataView来完成排序或筛选操作
if (strSort != null)
{
dv.Sort = strSort; //对DataView中的记录进行排序
}
if (strWhere != null)
{
dv.RowFilter = strWhere; //对DataView中的记录进行筛选,找到我们想要的记录
}
return dv;
}
catch (Exception)
{
return null;
}
} /// <summary>
/// 读取XML返回DataSet
/// </summary>
/// <param name="strXmlPath">XML文件相对路径</param>
public DataSet GetDataSetByXml(string strXmlPath)
{
try
{
DataSet ds = new DataSet();
ds.ReadXml(GetXmlFullPath(strXmlPath));
if (ds.Tables.Count > 0)
{
return ds;
}
return null;
}
catch (Exception)
{
return null;
}
}
#endregion
string s = File.ReadAllText(Server.MapPath("~/test.xml"));
string xmlStr = s.Replace("xs:", "");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
XmlNodeList names = xmlDoc.SelectNodes(@"//element[@name='user']//*/@name");
foreach (XmlNode name in names)
Response.Write(name.Value + "<br/>");
输出:
userName
userPass
id
string s = File.ReadAllText(Server.MapPath("~/test.xml"));
string xmlStr = s.Replace("xs:", "");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
XmlNodeList names = xmlDoc.SelectNodes(@"//element[@name='user']//*/@name");
string[] values = names.Cast<XmlNode>().Select(x => x.Value).ToArray<string>();
foreach (string value in values)
Response.Write(value + "<br/>");
输出
userName
userPass
id
XmlDocument xDoc = new XmlDocument();
xDoc.Load("..\\..\\test2.xml");
XmlNamespaceManager xnm = new XmlNamespaceManager(xDoc.NameTable);
xnm.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
XmlNodeList xNodes = xDoc.DocumentElement.SelectNodes("//*/xs:choice/xs:element/*//*[@name]", xnm);
foreach (XmlNode item in xNodes)
{
Console.WriteLine(item.Attributes["name"].Value);
}
//输出:
//userName
//userPass
//id我也帮你搞一个,注意带命名空间的需要使用XmlNamespaceManager。
然后使用SelectNodes(xPath)就可以找到你要的节点了。
这个是使用<xs:element name="user"/>来定位,我先前那个是使用双重节点来定位。xs:choice/xs:element