C# linq to Xml(复习用)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# linq to Xml(复习用)相关的知识,希望对你有一定的参考价值。
//把string字符串转换成xml格式 XDocument xd110 = XDocument.Parse(xml110); //加载指定路径下的文件到xml XDocument xDoc = XDocument.Load("Config.xml"); var query = from ele in xDoc.Elements("Config").Elements("Groups").Elements("Group") select ele; List<XElement> groups = query.ToList(); if (groups.Count > 0) { for (int i = 0; i < groups.Count; i++) { dicGroups.Add(groups[i].Attribute("Name").Value, groups[i].Value.PadLeft(3,‘0‘)); } }
private string ConvertCSCWXmlTC(XmlDocument doc, string msgType, string jqxzmc110, string jjdbh) { try { string HZDZ = doc.SelectSingleNode("MainMsg/MsgContent/JjXx/HZDZ").InnerText; //定义一个XDocument结构 XDocument myXDoc = new XDocument( new XDeclaration("1.0", "utf-8", null), new XElement("JXEXML", new XAttribute("version", "1.0"), new XElement("JXEJCJ", new XElement("JQXX", new XElement("JJDBH", jjdbh), new XElement("JQXZMS", jqxzmc110), new XElement("AFDD", HZDZ) )//JQXX )//JXEJCJ )//JXEXML ); return myXDoc.Declaration.ToString() + Environment.NewLine + myXDoc.ToString(); } catch (Exception ex) { ClassVar.WriteErrorLog("来自ConvertCSCWXmlTC方法中的错误信息" + Environment.NewLine + ex.ToString()); return ""; }
//XDocument格式转XmlDocument public XmlDocument ToXmlDocument(XDocument xDocument) { var xmlDocument = new XmlDocument(); using (var xmlReader = xDocument.CreateReader()) { xmlDocument.Load(xmlReader); } return xmlDocument; }
XmlDocument xml = new XmlDocument(); xml.Load("_119XZ.xml"); //内存中xml文档转字符串 string xmlStr=xml.OuterXml;
以上是关于C# linq to Xml(复习用)的主要内容,如果未能解决你的问题,请参考以下文章