C# 创建XML及使用它的值
作者:互联网
static void Main(string[] args)
{
//创建xml
XDocument employeeDoc =
new XDocument(
new XElement ("Employees",
new XElement ("Employee",
new XElement ("Name","Bob"),
new XElement ("PhoneNumber","88888888")),
new XElement ("Employee",
new XElement ("Name","Sun"),
new XElement("PhoneNumber", "77777777"),
new XElement("PhoneNumber", "66666666"))
)
);
// Console.WriteLine(employeeDoc);
//获取第一个名为“Employees”的子XElement
XElement root = employeeDoc.Element("Employees");
IEnumerable<XElement> employees = root.Elements();
foreach(XElement emp in employees )
{
//获取第一个名为"Name"的子XElement
XElement empNameNode = emp.Element("Name");
Console.WriteLine(empNameNode.Value );
//获取所有名为"PhoneNumber"的子元素
IEnumerable<XElement> empPhones = emp.Elements("PhoneNumber");
foreach ( XElement phone in empPhones )
{
Console.WriteLine("{0}",phone .Value);
}
}
xml表
使用xml的值
标签:XML,Console,Name,employeeDoc,C#,创建,XElement,PhoneNumber,new 来源: https://blog.csdn.net/qq_43393323/article/details/90207979