C#对XML、JSON等格式的解析
一、C#对XML格式数据的解析
1、用XMLDocument来解析
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("test.xml");
//创建新节点
XmlElement nn = xmlDocument.CreateElement("image");
nn.SetAttribute("imageUrl", "6.jpg");
XmlNode node = xmlDocument.SelectSingleNode("content/section/page/gall/folder");//定位到folder节点
node.AppendChild(nn);//附加新节点
//保存
xmlDocument.Save("test.xml");
2、用Linq to XML来解析
可以通过遍历,来获得你想要的节点的内容或属性
XElement root = XElement.Load("test.xml");
foreach (XAttribute att in root.Attributes())
{
root.Add(new XElement(att.Name, (string)att));
}
Console.WriteLine(root);
3、附一个详细点的例子
比如要解析如下的xml文件,将其转化为Ilist对象。