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- ...
随机推荐
- Python 解LeetCode:33. Search in Rotated Sorted Array
题目描述:在一个旋转数组中查找给定的值,其中旋转数组中不含重复值: 思路: 第一遍二分遍历,找到数组中最小值的索引: 第二遍分别对最小值左右两边的数组进行二分查找: class Solution(ob ...
- C++多线程基础学习笔记(十)
一.Windows临界区的基本用法 CRITICAL_SECTION my_winsc; //定义一个Windows的临界区,相当于一个mutex变量 InitializeC ...
- Scrapy里Selectors 四种基础的方法
在Scrapy里面,Selectors 有四种基础的方法xpath():返回一系列的selectors,每一个select表示一个xpath参数表达式选择的节点css():返回一系列的selector ...
- Linux Centos7 离线安装docker 【官网翻译和注释】
Centos7的Docker安装 需要一个维护版本的centos7,所以6不行. 卸载旧版本 旧版本的docker被称为 docker or docker-engine 如果存在请删除它们. sudo ...
- TPFanControl.ini
TPFanControl.ini 64位系统安装目录分为两种 64位用:C:\Program Files 32位用:C:\Program Files (x86) 64位系统系统目录分为两种 64位用: ...
- [NOIP10.6模拟赛]2.equation题解--DFS序+线段树
题目链接: 咕 闲扯: 终于在集训中敲出正解(虽然与正解不完全相同),开心QAQ 首先比较巧,这题是\(Ebola\)出的一场模拟赛的一道题的树上强化版,当时还口胡出了那题的题解 然而考场上只得了86 ...
- [转载]sklearn多分类模型
[转载]sklearn多分类模型 这篇文章很好地说明了利用sklearn解决多分类问题时的implement层面的内容:https://www.jianshu.com/p/b2c95f13a9ae.我 ...
- PS常识及技巧
常用格式 JPG:压缩 PNG:透明 GIF:动图 PSD:分层 分辨率 UI选择像素,印刷选择厘米 UI设计:72px 印刷分辨率必须为300 颜色模式UI网页设计:RGB 印刷类设计 ...
- python现状
自从官方宣布 2020 年 1 月后不再更新维护 Python2,已经有一大批开源软件将其抛弃.今天,抛弃 Python2 的名单上又多了一个重磅软件.Python2 是 Python 官方在 200 ...
- MongoDB divide 使用之mongotempalte divide
需求:求一组数的两个字段计算结果的平均值 如有一组这样的数: 列名1 列名2 列名3 第一组数 a 2 5 第二组数 b 4 8 按照列名1分组 ...