C# 生成XML 多级节点
直接上代码:
在应用程序中
class Program
{ //public static JsonServiceClient Service = new JsonServiceClient(ConfigInfo.GetSettingAsString("ServiceJsonServiceClient")); public static JsonServiceClient Service = new JsonServiceClient("http://localhost:65241/");
static void Main(string[] args)
{
Console.WriteLine("---------------开始 执行生成XML---------------"); CreateXmlFile(); Console.WriteLine("---------------结束 执行生成XML---------------"); Console.ReadKey();
} /// <summary>
/// 创建
/// </summary>
public static void CreateXmlFile()
{
XmlDocument xmlDoc = new XmlDocument();
//创建类型声明节点
XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
xmlDoc.AppendChild(node); //创建根节点
XmlElement xeRoot = xmlDoc.CreateElement("urlset");
//给节点属性赋值
xeRoot.SetAttribute("version", "1.0");
xeRoot.SetAttribute("name", "商品数据");
xmlDoc.AppendChild(xeRoot);
zgqp315_Common_Goods_XML_Status status = Service.Get<zgqp315_Common_Goods_XML_Status>(string.Format("selectzgqp315_Common_Goods_XML_Operation/{0}/{1}", , )); if (status.List.Count > )
{
for (int i = ; i < status.List.Count; i++)
{
xeRoot = xmlDoc.CreateElement("url");
XmlNode xn_urlset = xmlDoc.SelectSingleNode("urlset");
if (xn_urlset != null)
{
xn_urlset.AppendChild(xeRoot);
} CreateNode(xmlDoc, xeRoot, "loc", "http://www.baidu.com");
CreateNode(xmlDoc, xeRoot, "lastmod", DateTime.Now.ToLocalTime().ToString());
CreateNode(xmlDoc, xeRoot, "changefreq", "always");
CreateNode(xmlDoc, xeRoot, "priority", "1.0"); xeRoot = xmlDoc.CreateElement("data");
XmlNode xn_url = xmlDoc.SelectSingleNode("urlset").SelectNodes("url").Item(i);
if (xn_url != null)
{
xn_url.AppendChild(xeRoot);
} xeRoot = xmlDoc.CreateElement("dispaly");
XmlNode xn_data = xmlDoc.SelectSingleNode("urlset").SelectNodes("url").Item(i).SelectSingleNode("data");
if (xn_data != null)
{
xn_data.AppendChild(xeRoot);
} CreateNode(xmlDoc, xeRoot, "innerID", status.List[i].SKUCCId.ToString());
CreateNode(xmlDoc, xeRoot, "sourceTime", DateTime.Now.ToLocalTime().ToString());
CreateNode(xmlDoc, xeRoot, "productLine", "");
CreateNode(xmlDoc, xeRoot, "isDelete", "");
CreateNode(xmlDoc, xeRoot, "title", @"<![CDATA[" + status.List[i].GoodsName + "]]>");
CreateNode(xmlDoc, xeRoot, "poster", @"<![CDATA[]]>");
CreateNode(xmlDoc, xeRoot, "originalPrice", status.List[i].PricePure.ToString());
CreateNode(xmlDoc, xeRoot, "discountPrice", status.List[i].PricePure.ToString()); CreateNode(xmlDoc, xeRoot, "onlineTime", "");
CreateNode(xmlDoc, xeRoot, "totalSalesVolume", "");
CreateNode(xmlDoc, xeRoot, "totalReviewCounts", "");
CreateNode(xmlDoc, xeRoot, "goodReviewCounts", "");
CreateNode(xmlDoc, xeRoot, "rating", "9.6");
CreateNode(xmlDoc, xeRoot, "maxRating", "");
CreateNode(xmlDoc, xeRoot, "brand", status.List[i].GoodsName);
CreateNode(xmlDoc, xeRoot, "postageFlag", "");
CreateNode(xmlDoc, xeRoot, "certifiedFlag","");
CreateNode(xmlDoc, xeRoot, "compensationFlag","");
CreateNode(xmlDoc, xeRoot, "bestSellerFlag", "");
CreateNode(xmlDoc, xeRoot, "groupSale", "");
CreateNode(xmlDoc, xeRoot, "discountFlag", "");
CreateNode(xmlDoc, xeRoot, "discountStartTime", "");
CreateNode(xmlDoc, xeRoot, "discountEndTime", "");
CreateNode(xmlDoc, xeRoot, "newProductFlag", ""); CreateNode(xmlDoc, xeRoot, "status", "");
CreateNode(xmlDoc, xeRoot, "description", @"<![CDATA[]]>"); CreateNode(xmlDoc, xeRoot, "type", "配件");
CreateNode(xmlDoc, xeRoot, "hot", "");
CreateNode(xmlDoc, xeRoot, "maxHot", "");
CreateNode(xmlDoc, xeRoot, "comments", "Good!");
CreateNode(xmlDoc, xeRoot, "model", "");
CreateNode(xmlDoc, xeRoot, "origin", "");
CreateNode(xmlDoc, xeRoot, "urlForApp", @"<![CDATA[]]>");
CreateNode(xmlDoc, xeRoot, "urlForWeb", @"<![CDATA[]]>");
}
} try
{
xmlDoc.Save("d://商品_非3c商品.xml");
}
catch (Exception e)
{
//显示错误信息
Console.WriteLine(e.Message);
}
//Console.ReadLine(); }
/// <summary>
/// 创建节点
/// </summary>
/// <param name="xmldoc"></param> xml文档
/// <param name="parentnode"></param>父节点
/// <param name="name"></param> 节点名
/// <param name="value"></param> 节点值
///
public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
{
XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
node.InnerText = value;
parentNode.AppendChild(node);
}
}
效果图:
C# 生成XML 多级节点的更多相关文章
- python lxml库生成xml文件-节点命名空间问题
lxml库,处理xml很强大,官方文档:https://lxml.de/tutorial.html#namespaces 例如: 我们要生成如下格式的报文: <ttt:jesson xmlns: ...
- 自动生成XML空节点格式的差异
我们用C#开发了上位机配置软件,用C开发了嵌入式软件,然后他们之间的参数交互靠XML文件来沟通. C#中添加一个空的节点有以下几种情况. 不给节点的InnerText赋值: <root> ...
- C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML
using System; using System.Linq; using System.Xml.Linq; namespace Sample2 { class Program { static v ...
- java 解析并生成 XML
在 java 中使用 Dom4j 解析 XML 对 XML 文件的解析,通常使用的是 Dom4j 和 jdom 作为XML解析工具. 在此只介绍下 Dom4j 对 XML 文件的解析使用方法. 1. ...
- Android 使用xml序列化器生成xml文件
在<Android 生成xml文件>一文中使用流的形式写入xml格式文件,但是存在一定的问题,那就是在短信内容中不能出现<>之类的括号,本文使用xml序列化器来解决 xml序列 ...
- Android 生成xml文件
生成XML文件备份短信,其格式为: <?xml version="1.0" encoding="UTF-8" standalone="true& ...
- C# 生成xml文件
本篇文章旨在.net环境下生成xml文件,以控制台应用程序为例进行说明. 1.在vs中新建控制台应用程序CreateXml 2.CreateXmlFile:主要生成xml的函数 public void ...
- 使用XML序列化器生成XML文件和利用pull解析XML文件
首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...
- php生成xml的四种方法(转)
<?xml version="1.0" encoding="utf-8"?> <article> <item> <ti ...
随机推荐
- asp.net Global.asax 方法的使用和说明
Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法你可以使用这个文件实现应用程序安全性以及其它一些任务下面让我们详细看一 ...
- leetcode 14
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- CICS中设置程序常驻内存
CICS中设置程序常驻内存 Permanent=no 修改为Permanent=yse --------------------- 对CICS的参数进行调节,RD中
- 用IKVMC将jar转成dll供c#调用
用IKVMC将jar转成dll供c#调用 ikvmc c# dll jar 用IKVMC将jar转成dll供c#调用 前言 ikvmc介绍 ikvmc下载安装 下载并解压 设置环境变量 jar-> ...
- JavaScript高级 引用类型(二)《JavaScript高级程序设计(第三版)》
五.Function类型 是JS中最重要的一种引用类型 构造方式:(三种) 函数声明: 函数表达式定义: 函数构造器: 没有重载: 如果有两个相同函数名的函数,执行时,执行最近被定义的一次. 函数声明 ...
- 如何给WordPress安装百度统计代码
1.注册并登录百度统计,点击页面顶部的“网站中心”,然后点击右上角“+ 新增网站”,填写网站域名确定后,点击“复制代码”:2.登录 WordPress 后台,点击左侧导航栏“外观”里的“编辑”,然后点 ...
- 举例详解CSS中的的优先级
计算优先级 优先级是根据由每种选择器类型构成的级联字串计算而成的.他是一个对应匹配表达式的权重. 如果优先级相同,靠后的 CSS 会应用到元素上. 注意:元素在文档树中的位置是不会影响优先级的优先级顺 ...
- jquery.form.js实现将form提交转为ajax方式提交的使用方法
本文实例讲述了jquery.form.js实现将form提交转为ajax方式提交的方法.分享给大家供大家参考.具体分析如下: 这个框架集合form提交.验证.上传的功能. 这个框架必须和jquery完 ...
- POJ C++程序设计 编程题#1 编程作业—STL1
编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出结 ...
- string,stringbuilder,stringbuffer
String可以储存和操作字符串,即包含多个字符的字符数据.这个String类提供了存储数值不可改变的字符串. StringBuilder是线程不安全的,运行效率高,如果一个字符串变量是在方法里面定义 ...