XML operation

XElement root = new XElement("Params");
root.Add(new XAttribute("profile", "angela"));
root.Add(new XElement("id", "1"));
root.Add(new XElement("sex", "f"));
string ret = root.ToString();
Console.WriteLine(ret + "\n");
Console.WriteLine("============================================");
XElement doc = XElement.Parse(ret);
int count = root.Elements().Count();
Console.WriteLine("Count:" + count);
Console.WriteLine(doc.Attribute("profile").Value);
Console.WriteLine(doc.Element("id").Value);
Console.WriteLine(doc.Element("sex").Value);

Result

C# XML 例子-LMLPHP

04-10 14:06