LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#使用XDocument操作XML格式数据

admin
2025年7月24日 14:56 本文热度 170
XDocument是C#中LINQ to XML的核心类,它提供了一种更现代、更直观的方式来处理XML数据。相比传统的XmlDocument,XDocument更加简洁易用。

开发软件:Visual Studio 2019
核心组件:LINQ to XML (System.Xml.Linq)
辅助组件:System.Xml, System.IO
一.创建一个C#窗台程序用来演示C#操作XML,生成XML格式数据
<?xml version="1.0"?><res>  <Code>0</Code>  <Desc>成功</Desc>  <Info>    <Code>1</Code>    <Name>小明</Name>  </Info>  <Info>    <Code>2</Code>    <Name>大明</Name>  </Info>  <List>    <Info>      <Code>1</Code>      <Name>北京</Name>    </Info>    <Info>      <Code>2</Code>      <Name>上海</Name>    </Info>  </List></res>
代码:
            string[] username = { "小明""大明" };            string[] city = { "北京""上海" };
            // 创建基础文档结构            XDocument doc = new XDocument();            XDeclaration xDeclaration = new XDeclaration("1.0""utf-8"null);            doc.Declaration = xDeclaration;
            // 构建根元素            XElement xEres = new XElement("res");            XElement xDCode = new XElement("Code""0");            XElement xDDesc = new XElement("Desc""成功");            xEres.Add(xDCode);            xEres.Add(xDDesc);
            // 添加动态数据节点            for (int i = 0; i < username.Length; i++)            {                XElement xEInfo = new XElement("Info");
                XElement Code = new XElement("Code", (i + 1).ToString());                xEInfo.Add(Code);
                XElement Name = new XElement("Name", username[i]);                xEInfo.Add(Name);
                xEres.Add(xEInfo);            }
            // 添加嵌套列表结构            XElement List = new XElement("List");            for (int i = 0; i < city.Length; i++)            {                XElement xEInfo = new XElement("Info");
                XElement Code = new XElement("Code", (i + 1).ToString());                xEInfo.Add(Code);
                XElement Name = new XElement("Name", city[i]);                xEInfo.Add(Name);
                List.Add(xEInfo);            }            xEres.Add(List);
            doc.Add(xEres);
            richTextBox1.Text = XmlToString(doc).ToString();
xml转string
        public static string XmlToString(XDocument doc)        {            string xmlString;            using (var writer = new StringWriter())            {                using (var xmlWriter = XmlWriter.Create(writer))                {                    doc.WriteTo(xmlWriter);                }                xmlString = writer.ToString();            }            return xmlString;        }
二.把生成XML格式数据解析出来
代码:
            XDocument doc = XDocument.Parse(richTextBox1.Text);            XElement res = doc.Element("res");
            // 提取基础信息            string str_Code = res.Element("Code").Value;            string str_Desc = res.Element("Desc").Value;            richTextBox2.AppendText("Code:" + str_Code);            richTextBox2.AppendText("-");            richTextBox2.AppendText("Desc:" + str_Desc);            richTextBox2.AppendText("\r\n");
            // 遍历同级节点            var xEInfos = res.Elements("Info");
            foreach (XElement info in xEInfos)            {                string str_InfoCode = info.Element("Code").Value;                string str_InfoName = info.Element("Name").Value;
                richTextBox2.AppendText("userCode:" + str_InfoCode);                richTextBox2.AppendText("-");                richTextBox2.AppendText("userName:" + str_InfoName);                richTextBox2.AppendText("\r\n");            }
            // 处理嵌套结构            XElement List = res.Element("List");            var Infos = List.Elements("Info");
            foreach (XElement info in Infos)            {                string str_InfoCode = info.Element("Code").Value;                string str_InfoName = info.Element("Name").Value;
                richTextBox2.AppendText("cityCode:" + str_InfoCode);                richTextBox2.AppendText("-");                richTextBox2.AppendText("cityName:" + str_InfoName);                richTextBox2.AppendText("\r\n");            }


该文章在 2025/7/24 14:56:23 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved