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");
Linq to XML - C#生成XML的更多相关文章
- Java解析XML与生成XML文件
XML是eXtensible Markup Language(可扩展标记语言)的简写形式,它是一种元标记语言(meta-markup language),也就是说它没有一套能够适用于各个领域中所有用户 ...
- LINQ 从 CSV 文件生成 XML
本文参考:http://msdn.microsoft.com/zh-cn/library/bb387090.aspx 下面的代码对字符串数组执行 LINQ 查询. 在 C# 版本中,该查询使用 let ...
- C#操作XML存取创建XML
using System.Xml; #region 生成XML文档 /// <summary> /// /// </summary> /// <param name=& ...
- dom4j组装xml 以及解析xml
dom4j组装xml 以及解析xml: 1.下载dom4j的jar包,地址:https://dom4j.github.io/ 2.java代码: package test; import java.i ...
- LINQ to XML 从逗号分隔值 (CSV) 文件生成 XML 文件
参考:http://msdn.microsoft.com/zh-cn/library/bb387090.aspx 本示例演示如何使用 语言集成查询 (LINQ) 和 LINQ to XML 从逗号分隔 ...
- C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML
using System; using System.Linq; using System.Xml.Linq; namespace Sample2 { class Program { static v ...
- Linq to Xml读取复杂xml(带命名空间)
前言:xml的操作方式有多种,但要论使用频繁程度,博主用得最多的还是Linq to xml的方式,觉得它使用起来很方便,就用那么几个方法就能完成简单xml的读写.之前做的一个项目有一个很变态的需求:C ...
- WebAPI使用多个xml文件生成帮助文档
一.前言 上篇有提到在WebAPI项目内,通过在Nuget里安装(Microsoft.AspNet.WebApi.HelpPage)可以根据注释生成帮助文档,查看代码实现会发现是基于解析项目生成的xm ...
- WebAPI使用多个xml文件生成帮助文档(转)
http://www.cnblogs.com/idoudou/p/xmldocumentation-for-web-api-include-documentation-from-beyond-the- ...
随机推荐
- 常见三种加密(MD5、非对称加密,对称加密)
转载. https://blog.csdn.net/SSY_1992/article/details/79094556 任何应用的开发中安全都是重中之重,在信息交互异常活跃的现在,信息加密技术显得尤为 ...
- Photon Server初识(六) --- 客户端与服务端消息传递
前一章客户端与服务端连接成功,现在需要前后端进行数据传递. 一.前端发送消息.在项目Scripts目录中新建脚本 TestSer.cs.并挂载到相机上 二.客户端发送数据给服务端.编辑客户端代码 Te ...
- MySQL 子查询(一)
源自MySQL 5.7 官方手册 13.2.10 Subquery Syntax 〇.MySQL子查询介绍 子查询指的是嵌套在某个语句中的SELECT语句. MySQL支持标准SQL所要求的所有子查询 ...
- Nodejs:单线程为什么能支持高并发?
1.Nodejs是一个平台,构建在chrome的V8上(js语言解释器),采用事件驱动.非阻塞模型( c++库:libuv). 参考官方: Node.js is a platform built ...
- 设计模式风格<二>;消息总线
以前开发的动车模拟驾驶系统,有好几个软件(不在一台机器上),他们互相之间通信,因此每个软件要配置每个模块的IP和端口,就是每个模块都要知道别的模块的端口和IP. 这样有个重复的地方,B模块和C模块都要 ...
- python3.7 lxml4.2.5 etree xpath 的使用
#2019年10月14日11:08:49 from lxml import html etree = html.etree html = etree.HTML(response_dl.content) ...
- JavaScript Basics_Fundamentals Part 1_Numbers
Javascript Numbers 知识描述:JavaScript 只有一种数字类型,即数字(Number).数字可以带小数点,也可以不带,也就是整数和小数. 数字可以带小数点,也可以不带: Exa ...
- vue覆盖UI组件样式不生效
检查检查是不是加了scoped 在vue中,我们需要引用子组件,包括ui组件(element.iview). 但是在父组件中添加scoped之后,在父组件中书写子组件的样式是无效果的. 去掉scope ...
- BBPlus团队ALPHA冲刺博客(肖文恒)
ALPHA冲刺博客 第一天:https://www.cnblogs.com/bbplus/p/11931039.html 第二天:https://www.cnblogs.com/bbplus/p/11 ...
- 7.JSP技术
JSP和Servlet一样,都是用于开发动态web资源的技术 JSP的两个特性:1.写JSP代码就像是在写HTML代码,但JSP技术允许在页面中编写java代码(理解为一种特殊的HTML) 2.JSP ...