如何使用c#阅读并获取“姓名”,“年龄”和“性别”的值?
<Details>
<Name>xx</Name>
<age>yy</age>
<sex>zz</sex>
</Details>
答案 0 :(得分:4)
您可以使用LINQ to XML:
var xdoc = XDocument.Parse(xml);
var list = xdoc.Root.Descendants().Select(x => x.Value);
答案 1 :(得分:2)
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(@"c:\testapp\sample.xml");
// Root element
System.Xml.XmlElement root = doc.DocumentElement;
System.Xml.XmlElement nameElement =(System.Xml.XmlElement)root.ChildNodes[0];
string name = name.InnerText;
System.Xml.XmlElement ageElemnent =(System.Xml.XmlElement)root.ChildNodes[1];
string age = ageElemnent.InnerText;
System.Xml.XmlElement sexElemnent =(System.Xml.XmlElement)root.ChildNodes[2];
string sex= sexElemnent.InnerText;
答案 2 :(得分:0)
答案 3 :(得分:0)
XmlDocument
。阅读this XPath
查询。InnerText
。答案 4 :(得分:0)
您可以使用:
XmlDocument xml = new XmlDocument();
string myXml=@"<Details>
<Name>xx</Name>
<age>yy</age>
<sex>zz</sex>
</Details>";
xml.LoadXml(myXml);
XmlNodeList xnList = xml.SelectNodes("/Details");
foreach (XmlNode xn in xnList)
{
string name = xn["Name"].InnerText;
string age = xn["age"].InnerText;
string text = xn["sex"].InnerText;
Console.WriteLine("Name: {0} {1} {2}", name, age, text);
}
在询问
之前,您应该获得一些信息答案 5 :(得分:0)
LINq to XML:
XElement x = XElement.Load("In.xml");
var details = new
{
Name = x.Element("Name").Value,
Age = x.Element("age").Value,
Sex = x.Element("sex").Value
};
然后使用它,例如:
Console.WriteLine(details.Name);
Console.WriteLine(details.Age);
Console.WriteLine(details.Sex);