xml 创建加入

时间:2022-12-19 20:22:24
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>

4 个解决方案

#1


用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");

#2


如果是
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);???

#3


创建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];
        }

#4


引用 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"));

#1


用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");

#2


如果是
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);???

#3


创建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];
        }

#4


引用 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"));