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- ...
随机推荐
- php分页思路
<?php class page{ public $nowPage=1; public $perPage=10; public $showPage=10; public $totalPage; ...
- 并不对劲的CF1236D&E:Alice&Doll&UnfairGame
CF1236D Alice&Doll 题目描述 有一个机器人在一个\(n\times m\)的有\(k\)个障碍网格上移动,上北下南左西右东. 它一开始在第一行第一列,面朝东边.它在每个格子上 ...
- 超详细,新手都能看懂 !使用SpringBoot+Dubbo 搭建一个简单的分布式服务
来自:JavaGuide Github 地址:https://github.com/Snailclimb/springboot-integration-examples 目录: 使用 SpringBo ...
- margin:0 auto;生效条件
1.position:absolute下不生效 原因:position:absolute只能相对于父元素进行定位top.left定位,相当于浮在父元素上面,所以margin:0 auto;就没有了参考 ...
- ES6入门六:class的基本语法、继承、私有与静态属性、修饰器
基本语法 继承 私有属性与方法.静态属性与方法 修饰器(Decorator) 一.基本语法 class Grammar{ constructor(name,age){ //定义对象自身的方法和属性 t ...
- span元素
<span>标签属于行内元素(inline),所以无法设置高度和宽度: 如果需要改变其宽高,就需要将其转变为块体元素(block)或行内块体元素(inle-block)
- Linux环境下MySql安装和常见问题的解决
MySql安装 首先当然是要连接上linux服务器咯,然后就是下面的命令甩过去,梭哈,一通运行就是啦 梭哈 下载: sudo wget http://dev.mysql.com/get/mysql ...
- 如何使用Chrome开发者工具找到Marketing Cloud某个contact的guid
我们使用nodejs对contact进行修改时,需要指定待修改contact实例的guid. 这个guid属于technical属性,在Marketing Cloud UI上默认情况下不可见.如何找到 ...
- 15.SpringMVC核心技术-数据验证
在 Web 应用程序中,为了防止客户端传来的数据引发程序的异常,常常需要对数据进行验证. 输入验证分为客户端验证与服务器端验证.客户端验证主要通过 JavaScript 脚本进 行, 而服务器端验证则 ...
- C#验证控件使用方法及常用正则表达式例析(转)
ASP.NET为开发人员提供了一整套完整的服务器控件来验证用户输入的信息是否有效.这些控件如下: 1.RequiredFieldValidator:验证一个必填字段,如果这个字段没填,那么,将不能提交 ...