是这样的!要实现自定义一个XML 也就是内存中的,不是实际的XML文件,
xmlDocument 然后自己往这里面 给一个根结点,
然后再根结点下面指定添加子节点,最后将这个 xmlDocument 对象保存到数据库中某表中的某个字段中去,
结里在数据库表中的字段可以看到如下效果  <?xml version="1.0" encoding="utf-8"><newPricture><pic><href>href1</href></pic><pic><href>href1</href></pic><pic><href>href1</href></pic></newPricture>就如同一个真实的XML文件...

解决方案 »

  1.   

    XmlNode node = xmlDocument.CreateElement("节点名")
    xmlDocument.append(node);
    类似这样的写法
      

  2.   

    <?xml version="1.0" encoding="utf-8">
    这个声明 也要加进去呀!!!
      

  3.   

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.LoadXML("<?xml version=\"1.0\" encoding=\"utf-8\">");
    XmlNode node = xmlDocument.CreateElement("newPricture")
    xmlDocument.AppendNode(node); 
      

  4.   

    你说的是创建一个XML。我已前做过一次,能看懂多少,就是多少吧。 private void create_empty_movie_xml()
            {
                
                    XmlDocument xmldoc = new XmlDocument();
                    XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                    xmldoc.AppendChild(xmlnode);
                    XmlElement xmlelem = xmldoc.CreateElement("", "KeyFrames", "");
                    xmldoc.AppendChild(xmlelem);
                    XmlElement xmlSubElem;
                    XmlAttribute xmlAttr;
                    int totalSoundLength = 0;
                    for (int i = 0; i < dialogueSoundID.GetLength(0); i++)
                    {
                        xmlSubElem = xmldoc.CreateElement("", "Sounds", "");
                        xmlAttr = xmldoc.CreateAttribute("ID");
                        xmlAttr.Value = dialogueSoundID[i, 0].ToString();
                        xmlSubElem.Attributes.Append(xmlAttr);
                        if (dialogueSoundID[i, 1] == 1)
                        {
                            xmlAttr = xmldoc.CreateAttribute("soundPath");
                            xmlAttr.Value = "\\tasks\\" + GlobalValue.taskID.ToString() + "\\sounds" + "\\" + dialogueSoundID[i, 0].ToString() + ".wav";
                            xmlSubElem.Attributes.Append(xmlAttr);
                            xmlAttr = xmldoc.CreateAttribute("soundLength");
                            xmlAttr.Value = dialogueSoundID[i, 2].ToString();
                            totalSoundLength = totalSoundLength+ dialogueSoundID[i, 2];
                            xmlSubElem.Attributes.Append(xmlAttr);
                        }
                        else
                        {
                            xmlAttr = xmldoc.CreateAttribute("soundPath");
                            xmlAttr.Value = "";
                            xmlSubElem.Attributes.Append(xmlAttr);
                            xmlAttr = xmldoc.CreateAttribute("soundLength");
                            xmlAttr.Value = "0";
                            xmlSubElem.Attributes.Append(xmlAttr);
                        }
                        xmlAttr = xmldoc.CreateAttribute("playerID");
                        xmlAttr.Value = dialogueSoundID[i, 3].ToString();
                        xmlSubElem.Attributes.Append(xmlAttr);
                        xmlelem.AppendChild(xmlSubElem);
                    }
                    XmlElement xmlSpeElem = xmldoc.CreateElement("", "wholeSound", "");
                    XmlAttribute xmlAttrSpe = xmldoc.CreateAttribute("soundPath");
                    xmlAttrSpe.Value = "\\tasks\\" + GlobalValue.taskID.ToString() + "\\sounds" + "\\" + outputSoundFileName;
                    xmlSpeElem.Attributes.Append(xmlAttrSpe);
                    xmlAttrSpe = xmldoc.CreateAttribute("soundLength");
                    xmlAttrSpe.Value = totalSoundLength.ToString();
                    xmlSpeElem.Attributes.Append(xmlAttrSpe);
                    xmlelem.AppendChild(xmlSpeElem);
                    try
                    {
                        xmldoc.Save(taskPath + "\\movie_xml.xml");
                    }
                    catch (Exception er)
                    {
                        Console.WriteLine(er.Message);
                    }
                
            }
      

  5.   

    最后我要把 xmlDocument   这个对象保存到数据库的呀!!!
    然后数据库表的字段就可以看到一个XML格式的字符串。。
      

  6.   

    直接把XmlDocuemnt的InnerXML取出来不就可以了。。
    不会是保存数据库也不会把
      

  7.   

     StringBuilder sb = new StringBuilder();
                XmlWriterSettings setting = new XmlWriterSettings();
                setting.Encoding = Encoding.UTF8;
                setting.Indent = true;
                setting.OmitXmlDeclaration = false;
                XmlWriter writer = XmlWriter.Create(sb,setting);//XML编写器,有几个重载,根据自己需要选用
                writer.WriteStartElement("根节点");
                writer.WriteAttributeString("Name", "abc");            writer.WriteStartElement("子节点一");//开如子节点
                writer.WriteAttributeString("Name", "one");//属性,第一个参数是属性名,第二个参数是属性值
                writer.WriteEndElement();//结束子结点            writer.WriteStartElement("子节点二");
                writer.WriteAttributeString("Name", "two");
                writer.WriteEndElement();            writer.WriteEndElement();//结束根结点
                writer.Close();
                Console.WriteLine(sb.ToString());//显示XML内容显示:
    <?xml version="1.0" encoding="utf-16"?>
    <根节点 Name="abc">
      <子节点一 Name="one" />
      <子节点二 Name="two" />
    </根节点>
      

  8.   

    大家请看我是循环所有图片信息  myDiv_Img  是我获取到的所有图片地址
     XmlDocument xmlDoc = new XmlDocument(); 
    foreach (Match mImg in mcImg)
                    {
                    myDiv_Img = mImg.Groups["src"].Value + "\n";             
                      XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312",  null); 
      //手动添加一个声明              
      xmlDoc.AppendChild(dec); 
                    //创建根节点
                      XmlElement root = xmlDoc.CreateElement("Pricture"); 
                    xmlDoc.AppendChild(root); 
                    
                    XmlNode pic = xmlDoc.CreateElement("pic"); 
                    XmlElement title = xmlDoc.CreateElement("href"); 
                    title.InnerText = myDiv_Img;
                    pic.AppendChild(title); 
                    showXML = myDoc.InnerText;为 什么showXML 取不到值本来我是要取到值了然后插到数据库的。。  我是个新手希望大家指点下, 真的很急
      

  9.   

    你既然定义了节点pic那怎么不将它append到root下面呀.还有读取的时候怎么可以直接读myDoc呀。试试下面的root.AppendChild(pic); 
                XmlNode readNode = myDoc.SelectSingleNode("/Pricture/pic/href");
                showXML = readNode.InnerText; 
      

  10.   

    看错了,root.AppendChild(pic); 不用加了
      

  11.   

    不好意思刚有行代码少写了 root.AppendChild(pic); 
    这个我是有加进去的
    我就是不知道怎么读取 xmlDocument 的值
    试下 showXML = readNode.InnerText 看行不。
      

  12.   

    那下面也得改:XmlNode readNode = myDoc.SelectSingleNode("/pic/href");
                showXML = readNode.InnerText; 
      

  13.   

    楼上的朋友说的是可以取到值不过只是图片地址的值呀!! 例如取到的是 
    http://img10.360buyimg.com/S1/1627/19cc4bdc-239f-4f76-9325-dc156a1b7a8c.jpg
    而我要的是整个 xmlDocument 里面的所有内容保存在数据库
     例如
    <?xml version="1.0" encoding="utf-8"> <newPricture> <pic> <href>href1 </href> </pic> <pic> <href>http://img10.360buyimg.com/S1/1627/19cc4bdc-239f-4f76-9325-dc156a1b7a8c.jpg </href> </pic> <pic> <href>http://img10.360buyimg.com/S1/1627/19cc4bdc-239f-4f76-9325-dc156arewa8c.jpg </href> </pic> </newPricture> 我要实现这样的效果。。
      

  14.   

    看10楼的.也可以把刚才的改成。
                StringBuilder sb = new StringBuilder();
                XmlWriterSettings setting = new XmlWriterSettings();
                setting.Encoding = Encoding.UTF8;
                setting.Indent = true;
                setting.OmitXmlDeclaration = false;
                XmlWriter writer = XmlWriter.Create(sb, setting);
                //加入以前内容
                myDoc.WriteContentTo(writer);
                showXML = sb.ToString()
      

  15.   

     StringBuilder sb = new StringBuilder(); 
                XmlWriterSettings setting = new XmlWriterSettings(); 
                setting.Encoding = Encoding.UTF8; 
                setting.Indent = true; 
                setting.OmitXmlDeclaration = false; 
                XmlWriter writer = XmlWriter.Create(sb, setting); 
                
                XmlDocument xmlDoc = new XmlDocument(); 
               
                 XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312",  null); 
                 xmlDoc.AppendChild(dec); 
                    //创建根节点 
                    XmlElement root = xmlDoc.CreateElement("Pricture"); 
                    xmlDoc.AppendChild(root); 
                    
                    XmlNode pic = xmlDoc.CreateElement("pic"); 
                    root.AppendChild(pic);                 XmlElement title = xmlDoc.CreateElement("href"); 
                    title.InnerText = "hello"; 
                    pic.AppendChild(title);                XmlNode readNode = xmlDoc.SelectSingleNode("/Pricture/pic/href");                xmlDoc.WriteContentTo(writer);
                    writer.Close();
                    Console.WriteLine(sb.ToString());//显示XML内容
    完整代码。测试过了。结果:
    <?xml version="1.0" encoding="GB2312"?>
    <Pricture>
      <pic>
        <href>hello</href>
      </pic>
    </Pricture>
      

  16.   

    谢谢楼上的差不多可以了但是还有点小问题
    我运行完却是这种效果<?xml version="1.0" encoding="GB2312"?>
    <Pricture>
      <pic>
        <href>http://img10.360buyimg.com/S5/1874/3b0d418c-26f7-4ac4-a6a3-84f5a47e35b2.jpg</href>
      </pic>
    </Pricture><?xml version="1.0" encoding="GB2312"?>
    <Pricture>
      <pic>
        <href>http://img10.360buyimg.com/S5/3565/16387575-ca4a-4201-9227-fa094505df9b.jpg</href>
      </pic>
    </Pricture>这是我源代码 StringBuilder sb = new StringBuilder();
    foreach (Match mImg in mcImg)
                    {
                        myDiv_Img = mImg.Groups["src"].Value + "";
                        XmlWriterSettings setting = new XmlWriterSettings();
                        setting.Encoding = Encoding.UTF8;
                        setting.Indent = true;
                        setting.OmitXmlDeclaration = false;
                        XmlWriter writer = XmlWriter.Create(sb, setting);
                        XmlDocument xmlDoc = new XmlDocument();
                        XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
                        xmlDoc.AppendChild(dec);
                        //创建根节点 
                           XmlElement root = xmlDoc.CreateElement("Pricture");
                        xmlDoc.AppendChild(root);
                        XmlNode pic = xmlDoc.CreateElement("pic");
                        root.AppendChild(pic);
                        XmlElement title = xmlDoc.CreateElement("href");
                        title.InnerText =myDiv_Img;
                        pic.AppendChild(title);
                        XmlNode readNode = xmlDoc.SelectSingleNode("/Pricture/pic/href");
                        xmlDoc.WriteContentTo(writer);
                        writer.Close(); 
                    }
                     showXML = sb.ToString();
      

  17.   

    改下foreach (Match mImg in mcImg)的位置就行:
                    StringBuilder sb = new StringBuilder();
                    myDiv_Img = mImg.Groups["src"].Value + "";
                    XmlWriterSettings setting = new XmlWriterSettings();
                    setting.Encoding = Encoding.UTF8;
                    setting.Indent = true;
                    setting.OmitXmlDeclaration = false;
                    XmlWriter writer = XmlWriter.Create(sb, setting);
                    XmlDocument xmlDoc = new XmlDocument();
                    XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
                    xmlDoc.AppendChild(dec);
                    //创建根节点 
                    XmlElement root = xmlDoc.CreateElement("Pricture");
                    xmlDoc.AppendChild(root);
                    foreach (Match mImg in mcImg)
                    {
                    XmlNode pic = xmlDoc.CreateElement("pic");
                    root.AppendChild(pic);
                    XmlElement title = xmlDoc.CreateElement("href");
                    title.InnerText = myDiv_Img;
                    pic.AppendChild(title);
                    }
                    xmlDoc.WriteContentTo(writer);
                    writer.Close();
                
                    showXML = sb.ToString();