亚洲乱色熟女一区二区三区丝袜,天堂√中文最新版在线,亚洲精品乱码久久久久久蜜桃图片,香蕉久久久久久av成人,欧美丰满熟妇bbb久久久

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#使用XDocument操作XML格式數(shù)據(jù)

admin
2025年7月24日 14:56 本文熱度 898
XDocument是C#中LINQ to XML的核心類,它提供了一種更現(xiàn)代、更直觀的方式來處理XML數(shù)據(jù)。相比傳統(tǒng)的XmlDocument,XDocument更加簡潔易用。

開發(fā)軟件:Visual Studio 2019
核心組件:LINQ to XML (System.Xml.Linq)
輔助組件:System.Xml, System.IO
一.創(chuàng)建一個C#窗臺程序用來演示C#操作XML,生成XML格式數(shù)據(jù)
<?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 = { "北京""上海" };
            // 創(chuàng)建基礎(chǔ)文檔結(jié)構(gòu)            XDocument doc = new XDocument();            XDeclaration xDeclaration = new XDeclaration("1.0""utf-8"null);            doc.Declaration = xDeclaration;
            // 構(gòu)建根元素            XElement xEres = new XElement("res");            XElement xDCode = new XElement("Code""0");            XElement xDDesc = new XElement("Desc""成功");            xEres.Add(xDCode);            xEres.Add(xDDesc);
            // 添加動態(tài)數(shù)據(jù)節(jié)點            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);            }
            // 添加嵌套列表結(jié)構(gòu)            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轉(zhuǎn)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格式數(shù)據(jù)解析出來
代碼:
            XDocument doc = XDocument.Parse(richTextBox1.Text);            XElement res = doc.Element("res");
            // 提取基礎(chǔ)信息            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");
            // 遍歷同級節(jié)點            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");            }
            // 處理嵌套結(jié)構(gòu)            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 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved