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 ...
随机推荐
- 什么是动态语言 OC 的runtime
OC是一门 动态语言. 问题来了.什么是动态语言? 与之相对的静态语言? 学习C++的时候,记得一个名词:“运行时的动态绑定”. 这个是 “多态”的概念. 简单提一下:关键:类指针可以指向本类,或者其 ...
- Oracle:使用过程中的问题集锦
导读:在使用Oracle的过程中,又频繁的出问题.突然间就连接不上,各种报错了.在此,将问题给记录下来,方便以后查看. 一.ora 12514监听程序当前无法识别 之前一直链接使用的好好的,突然就连接 ...
- Spring 注解实体类中非数据库字段属性
解决办法:在属性的get方法上加上一段注解标识它是临时属性,不是数据库字段就OK @Transient public List<Reverts> getChildList() { retu ...
- iptable防火墙配置
/etc/sysconfig/iptables /etc/init.d/iptables {start|stop|restart|condrestart|status|panic|save} ipta ...
- 有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎
有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎 presto,hive,drill,calcite,sparksq
- leetcode 110
110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- phalcon框架学习之router
router定义 在DI中注册router的方法: $di->set('router', function(){ $router = new Phalcon\Mvc\Router(); $rou ...
- jquery图片轮播,单张图片轮播时间不同
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 分享一个jquery写的类似于百度的搜索框,(可动态配置,可单列或者table格式,可填充数据)
需求:类似于百度的搜索框,可配置,可单列可table格式,可填充数据.页面可多次使用,简单,易用. 想法:使用jquery,css,ajax,前台调用,后台返回json数据. jquery代码: va ...
- Overview Of Portal Registry And Content References
Portal Registry Each portal is defined by a portal registry.A portal registry has a tree-like struc ...