请教各位:如何用c#生成以下的XML文档? 拼字符串 OR 使用 System.XML 命名空间下的操作XML的类 XmlDocumnt 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 XmlDocument document = new XmlDocument(); XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "UTF-8", "");//xml文档的声明部分 document.AppendChild(declaration); XmlElement root = document.CreateElement("ns0", "Z_AVS_UPLOAD_WEIGHT_Request", "http://schemas.microsoft.com/BizTalk/2003"); document.AppendChild(root); XmlElement zwerks = document.CreateElement("ZWERKS"); zwerks.InnerText = "ZFM1"; root.AppendChild(zwerks); XmlElement tab1 = document.CreateElement("TAB1"); root.AppendChild(tab1); XmlElement zno = document.CreateElement("ZNO"); zno.InnerText = "13022101"; tab1.AppendChild(zno); XmlElement zorder = document.CreateElement("ZORDER"); zorder.InnerText = "2013238955"; tab1.AppendChild(zorder); XmlElement zweight = document.CreateElement("ZWEIGHT"); zweight.InnerText = "4140"; tab1.AppendChild(zweight); XmlElement tab2 = document.CreateElement("TAB1"); root.AppendChild(tab2); XmlElement zno2 = document.CreateElement("ZNO"); zno2.InnerText = "13022101"; tab2.AppendChild(zno2); XmlElement zorder2 = document.CreateElement("ZORDER"); zorder2.InnerText = "2013238955"; tab2.AppendChild(zorder2); XmlElement zweight2 = document.CreateElement("ZWEIGHT"); zweight2.InnerText = "4140"; tab2.AppendChild(zweight2); document.Save("test.xml");//将生成好的xml保存到test.xml文件中 又替你增加了一个linq to xml的解法。http://blog.csdn.net/chinacsharper/article/details/17268167 Linq to xml 非常方便 .net的XML基本操作是:1.new一个 System.Xml.XmlDocument对象。2.执行XmlDocument对象的Load方法加载XML文档。3.XmlDocument对象的DocumentElement对象可操作文档的根。4.可通过DocumentElement的ChildNodes属性获得根节点的所有子节点(XmlNode),子节点的ChildNodes属性可以获得该节点的所有子节点。5.节点(XmlNode)有多种类型。常用的有: 元素(Element),如<item/> 文本(Text),如<item>text<item/>中的text,通过InnerText属性获取这个文本。 通过节点的NodeType属性可以区分不同的节点。6.节点(XmlNode)的Attributes属性可以访问节点的所有属性(XmlAttribute),例如<item a="5"/>的a,通过Value属性获取值。7.通过XmlDocument对象的CreateElement方法去创建元素,通过节点(XmlNode)的AppendChild方法进行添加。8.通过XmlDocument对象的save方法进行存储。 非常感谢楼上各位的指点,2楼的代码也很实用。现还有两点需请教各位:1. 2楼的代码中为了生成两个tab1条目,用了:XmlElement tab1 = document.CreateElement("TAB1");XmlElement tab2 = document.CreateElement("TAB1");若是要生成n个"TAB1",比如说10个,34个,那前面那个tb1(tb2)该如何替换呢?2. 假设我在c盘根目录上生成了text.xml文件,对方需要我远程传送这个.xml文档到他的服务器去,并告知了服务器及相关的文件夹名称,如:\\100.101.102.135\AVS_FilePort\Input,我是否可用以下C#语句来传送文档:string path1,path2;path1="c:\test.xml";path2=@"\\100.101.102.135\AVS_FilePort\Input"; try{ File.Copy(path1,string.Format(path2\{0}", "test.xml")); }catch(Exception){} StringBuilder XmlStr = new StringBuilder(); XmlStr.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); XmlStr.Append("<ns0:Z_AVS_UPLOAD_WEIGHT_Request xmlns:ns0=\"http://schemas.microsoft.com/BizTalk/2003\">"); XmlStr.Append("<ZWERKS>ZFM1</ZWERKS>"); XmlStr.Append("<TAB1>"); XmlStr.Append("<ZNO>13022101</ZNO>"); XmlStr.Append("<ZORDER>2013238955</ZORDER>"); XmlStr.Append("<ZWEIGHT>4140</ZWEIGHT>"); XmlStr.Append("</TAB1>"); XmlStr.Append("<TAB1>"); XmlStr.Append("<ZNO>13022101</ZNO>"); XmlStr.Append("<ZORDER>2013239627</ZORDER>"); XmlStr.Append("<ZWEIGHT>4140</ZWEIGHT>"); XmlStr.Append("</TAB1>"); XmlStr.Append("</ns0:Z_AVS_UPLOAD_WEIGHT_Request>"); File.WriteAllText("d:\\1.xml", XmlStr.ToString());你还可以这样 最笨的方法StringBuilder拼接,但不推荐 0. using system.xml1. C# LINQ TO XML using system.xml.linq 2. VB xElementdim doc as xElement = _<?xml version="1.0" encoding="UTF-8"?>-<ns0:Z_AVS_UPLOAD_WEIGHT_Request xmlns:ns0="http://schemas.microsoft.com/BizTalk/2003"> <ZWERKS>ZFM1</ZWERKS> -<TAB1> <ZNO>13022101</ZNO> <ZORDER>2013238955</ZORDER> <ZWEIGHT>4140</ZWEIGHT> </TAB1> -<TAB1> <ZNO>13022101</ZNO> <ZORDER>2013239627</ZORDER> <ZWEIGHT>4140</ZWEIGHT> </TAB1></ns0:Z_AVS_UPLOAD_WEIGHT_Request> doc.save("demo.xml") 窗体关闭打开渐渐消失。 文本文件读写速度挺慢的 多线程 刷新界面 的方法讨论..........定时器刷界面 面试被问到的一个问题:你们是怎么做单元测试的?用什么单元测试工具了? 如何把 22_33 中的两个数字 分别赋给 int i,j 服务器上的一个应用程序通过3389启动不了 我该如何去选择我的未来? 如何存储TEXT类型的数据库!c#SQL2000 如何执行脚本文件 求助,在线等 多权限保存SQL的方法 c#问题
XmlDocument document = new XmlDocument(); XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "UTF-8", "");//xml文档的声明部分
document.AppendChild(declaration); XmlElement root = document.CreateElement("ns0", "Z_AVS_UPLOAD_WEIGHT_Request", "http://schemas.microsoft.com/BizTalk/2003");
document.AppendChild(root); XmlElement zwerks = document.CreateElement("ZWERKS");
zwerks.InnerText = "ZFM1";
root.AppendChild(zwerks); XmlElement tab1 = document.CreateElement("TAB1");
root.AppendChild(tab1); XmlElement zno = document.CreateElement("ZNO");
zno.InnerText = "13022101";
tab1.AppendChild(zno); XmlElement zorder = document.CreateElement("ZORDER");
zorder.InnerText = "2013238955";
tab1.AppendChild(zorder); XmlElement zweight = document.CreateElement("ZWEIGHT");
zweight.InnerText = "4140";
tab1.AppendChild(zweight); XmlElement tab2 = document.CreateElement("TAB1");
root.AppendChild(tab2); XmlElement zno2 = document.CreateElement("ZNO");
zno2.InnerText = "13022101";
tab2.AppendChild(zno2); XmlElement zorder2 = document.CreateElement("ZORDER");
zorder2.InnerText = "2013238955";
tab2.AppendChild(zorder2); XmlElement zweight2 = document.CreateElement("ZWEIGHT");
zweight2.InnerText = "4140";
tab2.AppendChild(zweight2); document.Save("test.xml");//将生成好的xml保存到test.xml文件中
1.new一个 System.Xml.XmlDocument对象。
2.执行XmlDocument对象的Load方法加载XML文档。
3.XmlDocument对象的DocumentElement对象可操作文档的根。
4.可通过DocumentElement的ChildNodes属性获得根节点的所有子节点(XmlNode),子节点的ChildNodes属性可以获得该节点的所有子节点。
5.节点(XmlNode)有多种类型。常用的有:
元素(Element),如<item/>
文本(Text),如<item>text<item/>中的text,通过InnerText属性获取这个文本。
通过节点的NodeType属性可以区分不同的节点。
6.节点(XmlNode)的Attributes属性可以访问节点的所有属性(XmlAttribute),例如<item a="5"/>的a,通过Value属性获取值。
7.通过XmlDocument对象的CreateElement方法去创建元素,通过节点(XmlNode)的AppendChild方法进行添加。
8.通过XmlDocument对象的save方法进行存储。
1. 2楼的代码中为了生成两个tab1条目,用了:
XmlElement tab1 = document.CreateElement("TAB1");
XmlElement tab2 = document.CreateElement("TAB1");
若是要生成n个"TAB1",比如说10个,34个,那前面那个tb1(tb2)该如何替换呢?2. 假设我在c盘根目录上生成了text.xml文件,对方需要我远程传送这个.xml文档到他的服务器去,并告知了服务器及相关的文件夹名称,如:\\100.101.102.135\AVS_FilePort\Input,我是否可用以下C#语句来传送文档:
string path1,path2;
path1="c:\test.xml";
path2=@"\\100.101.102.135\AVS_FilePort\Input";
try
{
File.Copy(path1,string.Format(path2\{0}", "test.xml"));
}
catch(Exception)
{
}
XmlStr.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
XmlStr.Append("<ns0:Z_AVS_UPLOAD_WEIGHT_Request xmlns:ns0=\"http://schemas.microsoft.com/BizTalk/2003\">");
XmlStr.Append("<ZWERKS>ZFM1</ZWERKS>");
XmlStr.Append("<TAB1>");
XmlStr.Append("<ZNO>13022101</ZNO>");
XmlStr.Append("<ZORDER>2013238955</ZORDER>");
XmlStr.Append("<ZWEIGHT>4140</ZWEIGHT>");
XmlStr.Append("</TAB1>");
XmlStr.Append("<TAB1>");
XmlStr.Append("<ZNO>13022101</ZNO>");
XmlStr.Append("<ZORDER>2013239627</ZORDER>");
XmlStr.Append("<ZWEIGHT>4140</ZWEIGHT>");
XmlStr.Append("</TAB1>");
XmlStr.Append("</ns0:Z_AVS_UPLOAD_WEIGHT_Request>");
File.WriteAllText("d:\\1.xml", XmlStr.ToString());你还可以这样
2. VB xElement
dim doc as xElement = _
<?xml version="1.0" encoding="UTF-8"?>
-<ns0:Z_AVS_UPLOAD_WEIGHT_Request xmlns:ns0="http://schemas.microsoft.com/BizTalk/2003">
<ZWERKS>ZFM1</ZWERKS>
-<TAB1>
<ZNO>13022101</ZNO>
<ZORDER>2013238955</ZORDER>
<ZWEIGHT>4140</ZWEIGHT>
</TAB1>
-<TAB1>
<ZNO>13022101</ZNO>
<ZORDER>2013239627</ZORDER>
<ZWEIGHT>4140</ZWEIGHT>
</TAB1>
</ns0:Z_AVS_UPLOAD_WEIGHT_Request> doc.save("demo.xml")