我有一个XML,我想写两个方法。一个方法得到info里两个节点code、和name的值。
一个方法是得到menus里所有Item里的属性和Value值。
<?xml version="1.0" encoding="UTF-8"?>
<Config>
<info>
<code>1</code>
<name>1.0.0.0</name>
</info>
<Category>
<menus>
<Item Name="XiTong">系统</Item>
<Item Name="Bianji">编辑</Item>
<Item Name="SoSuo">搜索</Item>
<Item Name="ShiTu">视图</Item>
<Item Name="Bangzhu">帮助</Item>
</menus>
</Category>
</Config>
非常感谢各位!
一个方法是得到menus里所有Item里的属性和Value值。
<?xml version="1.0" encoding="UTF-8"?>
<Config>
<info>
<code>1</code>
<name>1.0.0.0</name>
</info>
<Category>
<menus>
<Item Name="XiTong">系统</Item>
<Item Name="Bianji">编辑</Item>
<Item Name="SoSuo">搜索</Item>
<Item Name="ShiTu">视图</Item>
<Item Name="Bangzhu">帮助</Item>
</menus>
</Category>
</Config>
非常感谢各位!
string str = "";
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "code":
ht.Add("code", reader.Value);
break;
case "name":
ht.Add("name", reader.Value);
break;
default:
break;
}
}
}这样的方式取不到值啊!reader.Value
private void Test()
{ XElement x = XElement.Load(@"e:\1.xml");
var infos = from node in x.Descendants("info")
select new
{
code = node.Element("code").Value,
name = node.Element("name").Value,
}; var menus = from node in x.Element("Category").Element("menus").Descendants("Item")
select new
{
name = node.Attribute("Name").Value,
text = node.Value,
};
}如果你的XML已经在内存中,例如是一个字符串,可以这样进行读取XElement x = XElement.Parse("<?xml version=...");
给你个流氓的写法DataSet ds =new DataSet();
ds.ReadXml(你的文件或流);DataTable info =ds.Tables["info"];
DataTable menus=ds.Tables["Item"];
拼写可能有错误,纯手打 王甜甜同学
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode("/Config/info/name");
if (xn != null)
Console.WriteLine(xn.InnerXml);
XmlDocument doc=new XmlDocument();
doc.Load(@"c:\test.xml");string path="/Config/info/code";
XmlNode node=doc.SelectSingleNode(path);
string val=node.Value;//code的值path="/Config/info/name";
node=doc.SelectSingleNode(path);
val=node.Value; //name的值path="/Config/Category/menus/Item";
XmlNodeList nodeList=doc.SelectNodes(path);foreach(XmlNode sub in nodeList)
{
string attr=sub.Attributes["Name"].Value ;//Item属性
val=sub.Value; //Item值
}
foreach (XmlNode node in nodeList)
{
foreach (XmlNode childnode in node.ChildNodes)
{
string aaa=childnode.Name;
string bbb=childnode.InnerText;
}
}
xmldoc.LoadXml(xmlstr);
//获取指定节点
string code = xmldoc.SelectSingleNode("/Config/info/code").InnerText;
string name = xmldoc.SelectSingleNode("/Config/info/name").InnerText;
//获取全部Item节点
XmlNodeList nodes = xmldoc.SelectNodes("/Config/Category/menus/Item");
List<string[]> list = new List<string[]>(nodes.Count);
foreach(XmlNode node in nodes)
{
string[] strarr = new string[2];
strarr[0] = node.InnerText;
strarr[1] = node.Attributes["Name"].Value;
//strarr[1] = node.SelectSingleNode("@Name").Value;
list.Add(strarr);
}
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode("/Config/info/name");//code类似..
if (xn != null)
Console.WriteLine(xn.InnerXml); XmlNode xn2 = doc.SelectSingleNode("/Config/Category/menus");
foreach (XmlNode xno in xn2.ChildNodes)
{
Console.WriteLine(xno.Attributes["Name"].Value.ToString());//xitong
Console.WriteLine(xno.InnerText);//系统
}
<info>
<code>1</code>
<name>1.0.0.0</name>
</info>
<Category>
<menus>
<Item Name=""XiTong"">系统</Item>
<Item Name=""Bianji"">编辑</Item>
<Item Name=""SoSuo"">搜索</Item>
<Item Name=""ShiTu"">视图</Item>
<Item Name=""Bangzhu"">帮助</Item>
</menus>
</Category>
</Config>"; XmlDocument xml = new XmlDocument();
xml.LoadXml(str); //第一个
string infocode = "";
string infoname = "";
{
XmlElement xe = (XmlElement)xml.SelectSingleNode("//info/code");
if (xe != null) infocode = xe.InnerXml;
}
{
XmlElement xe = (XmlElement)xml.SelectSingleNode("//info/name");
if (xe != null) infoname = xe.InnerXml;
} //第二个
foreach (XmlElement xe in xml.SelectNodes("//Category/menus//Item"))
{
//属性值
string name = xe.GetAttribute("Name"); //节点值
string inner = xe.InnerXml;
}
{
string xmlFile = @"test.xml";
XmlDocument doc = new XmlDocument();
if (File.Exists(xmlFile))
{
doc.Load(xmlFile);
XmlNode mainNode= doc.SelectSingleNode(nodepath);
if (mainNode[nodename] != null)
return mainNode[nodename].InnerText;
}
return ""; }
private List<NameValue> GetNodeInfo(string nodepath, string nodename,string attrname)
{
List<NameValue> retLst = new List<NameValue>();
string xmlFile = @"test.xml";
XmlDocument doc = new XmlDocument();
if (File.Exists(xmlFile))
{
doc.Load(xmlFile);
XmlNode mainNode = doc.SelectSingleNode(nodepath);
if (mainNode == null)
return retLst;
if (mainNode.ChildNodes.Count > 0)
{
foreach (XmlNode node in mainNode.ChildNodes)
{
if (node.Name.Equals(nodename))
{
string value = node.InnerText;
string name="";
if (node.Attributes[attrname] != null)
name = node.Attributes[attrname].Value;
NameValue info = new NameValue(name,value);
retLst.Add(info);
}
}
}
}
return retLst;
}
class NameValue
{
private string mName="";
private string mValue="";
public string Name
{
get{return mName;}
set{mName=value;}
}
public string Value
{
get{return mValue;}
set{ mValue=value;}
}
public NameValue(string name,string value)
{
mName=name;
mValue=value;
}
}