Linq to XML - C#生成XML
1.System.Xml.XmlDocument
XML file转成字符串
string path3 = @"C:\Users\test.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path3);
string xmlStr = xmlDoc.InnerXml
查找结点,需加上命名空间
xmlDoc.Load(path);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
string startNode = "/soap:Envelope/soap:Body/Test";
XmlNodeList nodeList = xmlDoc.SelectNodes(startNode, nsMgr);
真心麻烦。。。
2. System.Xml.Serialization
从object到XML字符串一气生成,非常好用,果断点赞!
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Serialization;
public static string ObjToXmlStr(Object obj) { string xmlStr = string.Empty; //XmlSerializer xmlser = new XmlSerializer(obj.GetType()); //using (StringWriter sw = new StringWriter()) //{ // xmlser.Serialize(sw, obj); // xmlStr = sw.ToString(); //}; using(MemoryStream ms = new MemoryStream()){ StreamWriter sw = new StreamWriter(ms); XmlWriterSettings setting = new XmlWriterSettings(); setting.Encoding = Encoding.UTF8 ; setting.Indent = true ; XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); using (XmlWriter writer = XmlWriter.Create(sw, setting)) { XmlSerializer xmlser = new XmlSerializer(obj.GetType()); xmlser.Serialize(writer, obj ,ns); writer.Flush(); writer.Close(); } using (StreamReader sr = new StreamReader(ms)) { ms.Position = 0; xmlStr = sr.ReadToEnd(); sr.Close(); } } return xmlStr; }
public static Object XmlStrToObj(string xmlStr) { Object obj = new Object(); using (StringReader sr = new StringReader(xmlStr)) { XmlSerializer xmldes = new XmlSerializer(typeof(SendPayslipRequest)); obj = xmldes.Deserialize(sr); } return obj; }
public static XElement GetXEleByName(IEnumerable<XElement> xEles , string eleName) { var q = from item in xEles where item.Name.LocalName == eleName select item; return q.FirstOrDefault(); }
public static void SetXEleValueByName(IEnumerable<XElement> xEles , string eleName , string eleValue) { XElement xele = GetXEleByName(xEles, eleName); if(xele != null) xele.Value = eleValue; }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; using System.Runtime.Serialization; namespace VML.Employee.DataContracts { [XmlRoot("sendPayslipRequest", Namespace = "http://www.abc.com/Test.xsd")] public class Test { [XmlAttributeAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] public string xsi = "http://www.abc.com/Test.xsd"; [DataMember] [XmlElement("TestID")] public String TestID { get; set; } [DataMember] [XmlElement("TestName")] public String TestName { get; set; } } }
根据test生成string
Test test = new Test(); test.TestID = "ID1"; test.TestName = "TestName"; string testXmlStr = XmlHelper.ObjToXmlStr(test);
生成的xml string:
<?xml version="1.0" encoding="utf-8"?> <sendPayslipRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abc.com/Test.xsd" xmlns="http://www.abc.com/Test.xsd"> <TestID>ID1</TestID> <TestName>TestName</TestName> </sendPayslipRequest>
定位到某个element:
XDocument xDoc2 = XDocument.Parse(testXmlStr);
IEnumerable<XElement> xEles2 = xDoc2.Root.Elements();
XElement xele = XmlHelper.GetXEleByName(xEles2, "TestID");
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!