xmldoc = new XmlDocument() ;
xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration , "" , "" ) ;
xmldoc.AppendChild( xmlnode ) ;
xmlelem = xmldoc.CreateElement( "" , "root" , "" ) ;
xmldoc.AppendChild( xmlelem ) ;
xmlelem2 = xmldoc.CreateElement("img" ) ;
//想在这里给IMG加几个属性
xmldoc.ChildNodes.Item(1).AppendChild( xmlelem2 ) ;
xmldoc.Save( "c:\\data.xml" ) ; 我想让输出的样子是这个样子,要怎么写
<?xml version="1.0"?>
<root>
  <img label="布达拉宫" id="72" picurl="2005-12-1\m33535143.jpg" picurl100="2005-12-1\m33535143.jpg" picurl500="2005-12-1\m33535143.jpg" picrar="0"/>
</root>

解决方案 »

  1.   

    XmlDocument doc = new XmlDocument();
                XmlDeclaration xd = doc.CreateXmlDeclaration("1.0", "", "");
                doc.AppendChild(xd);
                XmlElement elem = doc.CreateElement("root");
                doc.AppendChild(elem);
                XmlElement e1 = doc.CreateElement("img");
                e1.SetAttribute("label", "布达拉宫");
                e1.SetAttribute("id", "72");
                e1.SetAttribute("picurl",@"2005-12-1\m33535143.jpg");
                e1.SetAttribute("picurl100",@"2005-12-1\m33535143.jpg");
                e1.SetAttribute("picurl500",@"2005-12-1\m33535143.jpg");
                e1.SetAttribute("picrar","0");
                elem.AppendChild(e1);
                doc.Save(@"..\..\newXml.xml");
      

  2.   

    XmlDoucment  xmldoc = new XmlDocument() ;
    xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration , "" , "" ) ;
    xmldoc.AppendChild( xmlnode ) ;
    xmlelem = xmldoc.CreateElement( "" , "root" , "" ) ;
    xmldoc.AppendChild( xmlelem ) ;
    xmlelem2 = xmldoc.CreateElement("img" ) ;
    //想在这里给IMG加几个属性
    xmlelem。setAttribute(。);
    xmldoc.ChildNodes.Item(1).AppendChild( xmlelem2 ) ;
    xmldoc.Save( "c:\\data.xml" ) ; 
      

  3.   

    http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/episode.aspx?newsID=1242450ASP.NET中的XML在如今的网络编程中,XML的使用是相当广泛的。那么在ASP.NET中如何灵活使用XML呢?这次讲座我们将给大家详细介绍这方面的知识。  Quick Info  
    讲师:  邵志东  
    讲课时间:  2006年3月10日 10:00-11:30 
    产品:  Visual Studio 
    技术:  ASP.NET 
    技术等级:  200