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- ...
随机推荐
- vue去掉链接中的#
在router.js中修改, const router = new VueRouter({ mode: 'history', routes: [...] })
- java--键盘输入任意数字进行求和
思路,我将键盘输入的数放入数组,然后便利数组进行求和 package com.test.day01; import java.util.Scanner; public class Test { pub ...
- 剑指offer23:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。输出Yes OR No。
1 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 2 思路和方法 二叉搜索树:二叉查找树(Bin ...
- 爬虫-selenium 模块-02
目录 selenium 模块 chromedriver 浏览器驱动下载与存放 PhantomJS 无界面浏览器 标签元素查找方法 xpath 格式用法 获取标签属性 等待元素被加载 元素交互操作 点击 ...
- 怎样修改 VS Code 主题?
方法1. 点击左上角 File > Preferences > Color Theme. 方法2. 使用快捷键: Ctrl + K , Ctrl + T PS: 查询各种操作的快捷键可以 ...
- 适配方案(五)适配的基础知识之设备像素比 dpr 与 1px 物理像素
设备像素比 dpr 与 1px 物理像素 物理像素(physical pixel) 手机屏幕上显示的最小单元,该最小单元具有颜色及亮度的属性可供设置,iphone6.7.8 为:750 * 1334, ...
- [转载]SSD原理与实现
[转载]SSD原理与实现 这里只mark一下,对原论文讲解的很好的博文 https://zhuanlan.zhihu.com/p/33544892 这里有一个关于SSD的很好的程序实现,readme里 ...
- python numpy 删除array指定位置的元素
如图:设计一个数组或者tuple,其中的元素是True或False,那么在False位置上的元素就会被删掉 索引的元素还可以是int型的数,这时候就代表,将原来的数组中指定位置的数放在当前的位置,且索 ...
- 解决设置了display:none的元素,会先展示再隐藏
问题:元素明明设置了display:none,但是在刷新页面的时候却会先显示了出来,然后才会隐藏,实现display:none 原因:由于元素渲染的时候,样式还没有应用上去,导致的 解决办法:使用内联 ...
- Dubbo 配置参数
关闭启动检查 在dubbo多模块项目启动的时候为了并行启动多个服务,缩短启动时间,需要解除模块之间的依赖检测 dubbo.consumer.check=false @Reference(check = ...