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 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;

  

 

转载于:https://www.cnblogs.com/KQNLL/p/5223700.html

你可能感兴趣的:(C# linq to Xml(复习用))