码迷,mamicode.com
首页 > Windows程序 > 详细

C# linq to Xml(复习用)

时间:2016-02-27 23:21:14      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

 
//把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(复习用)

标签:

原文地址:http://www.cnblogs.com/KQNLL/p/5223700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!