您的位置:首页 > 编程语言 > C#

Linq to XML - C#生成XML

2014-05-07 16:37 411 查看
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");



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: