xml 创建加入

dys_198102 2014-08-06 05:11:46
var doc = XDocument.Parse(xml);
ResponseMessageBase responseMessage = null;
var msgType = (ResponseMsgType)Enum.Parse(typeof(ResponseMsgType), doc.Root.Element("MsgType").Value, true);

创建xml时,怎么把

<?xml version="1.0" encoding="utf-8"?>
补全。。。,生成返回的数据如下:

<xml>
<ToUserName><![CDATA[oYp8vuNHDFgXcJBsw8aJDUTelpLI]]></ToUserName>
<FromUserName><![CDATA[gh_8aff2b7ccf85]]></FromUserName>
<CreateTime>1407315979</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[测试]]></Content>
</xml>
...全文
1386 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunny906 2014-08-08
  • 打赏
  • 举报
回复
引用 2 楼 dys_198102 的回复:
doc.Declaration = new XDeclaration("1.0", "utf-8", null);???
是的

            var doc = new XDocument();
            doc.Declaration = new XDeclaration("1.0", "utf-8", "");
            doc.Add(new XElement("xml"));

            var doc = new XDocument(new XDeclaration("1.0", "utf-8", ""));
            doc.Add(new XElement("xml"));
xiao_2528276651 2014-08-08
  • 打赏
  • 举报
回复
创建XML文件加入<?xml version="1.0" encoding="utf-8"?> //加载XML文件绑定DataGrieView显示内容 //创建XML文件 private void button1_Click(object sender, EventArgs e) { XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement(textBox1.Text, new XElement(textBox2.Text, new XAttribute(textBox3.Text, textBox10.Text), new XElement(textBox4.Text, textBox5.Text), new XElement(textBox6.Text, textBox7.Text), new XElement(textBox8.Text, textBox9.Text)) ) ); doc.Save(strPath); groupBox1.Enabled = false; getXmlInfo(); } //加载XML文件 private void Form1_Load(object sender, EventArgs e) { if (File.Exists(strPath)) { groupBox1.Enabled = false; getXmlInfo(); }//CodeGo.net/ else groupBox1.Enabled = true; } //自 定义getXmlInfo方法 private void getXmlInfo() { DataSet myds = new DataSet(); myds.ReadXml(strPath); dataGridView1.DataSource = myds.Tables[0]; }
dys_198102 2014-08-07
  • 打赏
  • 举报
回复
如果是 var doc = new XDocument(); doc.Add(new XElement("xml")); // 怎么加上<?xml version="1.0" encoding="utf-8"?> doc.Declaration = new XDeclaration("1.0", "utf-8", null);???
sunny906 2014-08-06
  • 打赏
  • 举报
回复
用XDeclaration

            XDocument doc = new XDocument(
                new XDeclaration("1.0", "utf-8", null),
                new XElement("xml",
                    new XElement("ToUserName", "<![CDATA[oYp8vuNHDFgXcJBsw8aJDUTelpLI]]>"),
                    new XElement("FromUserName", "<![CDATA[gh_8aff2b7ccf85]]>")
                    )
                );

            doc.Save("C:\\1.xml");

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧