关于C#中,用后台代码创建xml文件··
要创建的文件如下<?xmlversion="1.0"?><CATALOG><CD><TITLE>EmpireBurlesque</TITLE><ARTIST>BobDyl...
要创建的文件如下
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tylor</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>RCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1982</YEAR>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary More</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin redords</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1990</YEAR>
</CD>
<CD>
<TITLE>Eros</TITLE>
<ARTIST>Eros Ramazzotti</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>BMG</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1997</YEAR>
</CD>
</CATALOG>
需要具体可用的示例代码:而且请看清楚这一共是3级节点··不要那种2级的 展开
<?xml version="1.0"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tylor</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Greatest Hits</TITLE>
<ARTIST>Dolly Parton</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>RCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1982</YEAR>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary More</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin redords</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1990</YEAR>
</CD>
<CD>
<TITLE>Eros</TITLE>
<ARTIST>Eros Ramazzotti</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>BMG</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1997</YEAR>
</CD>
</CATALOG>
需要具体可用的示例代码:而且请看清楚这一共是3级节点··不要那种2级的 展开
4个回答
展开全部
XmlDocument xmldoc = new XmlDocument();
//声明节
XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.AppendChild(dec);
//加入一个根节点
XmlElement oneNode = xmldoc.CreateElement("CATALOG");
//创建节点
XmlElement twoNode = xmldoc.CreateElement("CD");
XmlElement twoNodeone = xmldoc.CreateElement("TITLE");
twoNodeone.InnerText="Empire Burlesque";
twoNode.AppendChild(twoNodeone);//添加到CD节点下面
XmlElement twoNodeone1 = xmldoc.CreateElement("ARTIST");
twoNodeone1.InnerText = "Bob Dylan";
twoNode.AppendChild(twoNodeone1);//添加到CD节点下面
XmlElement twoNodeone2 = xmldoc.CreateElement("COUNTRY");
twoNodeone2.InnerText = "USA";
twoNode.AppendChild(twoNodeone2);//添加到CD节点下面
XmlElement twoNodeone3 = xmldoc.CreateElement("COMPANY");
twoNodeone3.InnerText = "Columbia";
twoNode.AppendChild(twoNodeone3);//添加到CD节点下面
XmlElement twoNodeone4 = xmldoc.CreateElement("YEAR");
twoNodeone4.InnerText = "10.90";
twoNode.AppendChild(twoNodeone4);//添加到CD节点下面
XmlElement twoNodeone5 = xmldoc.CreateElement("PRICE");
twoNodeone5.InnerText = "1985";
twoNode.AppendChild(twoNodeone5);//添加到CD节点下面
oneNode.AppendChild(twoNode);//添加到CATALOG节点下面
//创建节点,第二个CD节点
XmlElement twoNode1 = xmldoc.CreateElement("CD");
XmlElement twoNodeone123 = xmldoc.CreateElement("TITLE");
twoNodeone123.InnerText = "Empire Burlesque";
twoNode1.AppendChild(twoNodeone123);//添加到CD节点下面
XmlElement twoNodeone11 = xmldoc.CreateElement("ARTIST");
twoNodeone11.InnerText = "Bob Dylan";
twoNode1.AppendChild(twoNodeone11);//添加到CD节点下面
XmlElement twoNodeone22 = xmldoc.CreateElement("COUNTRY");
twoNodeone22.InnerText = "USA";
twoNode1.AppendChild(twoNodeone22);//添加到CD节点下面
XmlElement twoNodeone33 = xmldoc.CreateElement("COMPANY");
twoNodeone33.InnerText = "Columbia";
twoNode1.AppendChild(twoNodeone33);//添加到CD节点下面
XmlElement twoNodeone44 = xmldoc.CreateElement("YEAR");
twoNodeone44.InnerText = "10.90";
twoNode1.AppendChild(twoNodeone44);//添加到CD节点下面
XmlElement twoNodeone45 = xmldoc.CreateElement("PRICE");
twoNodeone45.InnerText = "1985";
twoNode1.AppendChild(twoNodeone45);//添加到CD节点下面
oneNode.AppendChild(twoNode1);//添加到CATALOG节点下面
xmldoc.AppendChild(oneNode);
xmldoc.Save(Server.MapPath("")+"/1.xml");//保存xml
注意:cd节点可以通过循环添加到CATALOG节点下面
//声明节
XmlDeclaration dec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.AppendChild(dec);
//加入一个根节点
XmlElement oneNode = xmldoc.CreateElement("CATALOG");
//创建节点
XmlElement twoNode = xmldoc.CreateElement("CD");
XmlElement twoNodeone = xmldoc.CreateElement("TITLE");
twoNodeone.InnerText="Empire Burlesque";
twoNode.AppendChild(twoNodeone);//添加到CD节点下面
XmlElement twoNodeone1 = xmldoc.CreateElement("ARTIST");
twoNodeone1.InnerText = "Bob Dylan";
twoNode.AppendChild(twoNodeone1);//添加到CD节点下面
XmlElement twoNodeone2 = xmldoc.CreateElement("COUNTRY");
twoNodeone2.InnerText = "USA";
twoNode.AppendChild(twoNodeone2);//添加到CD节点下面
XmlElement twoNodeone3 = xmldoc.CreateElement("COMPANY");
twoNodeone3.InnerText = "Columbia";
twoNode.AppendChild(twoNodeone3);//添加到CD节点下面
XmlElement twoNodeone4 = xmldoc.CreateElement("YEAR");
twoNodeone4.InnerText = "10.90";
twoNode.AppendChild(twoNodeone4);//添加到CD节点下面
XmlElement twoNodeone5 = xmldoc.CreateElement("PRICE");
twoNodeone5.InnerText = "1985";
twoNode.AppendChild(twoNodeone5);//添加到CD节点下面
oneNode.AppendChild(twoNode);//添加到CATALOG节点下面
//创建节点,第二个CD节点
XmlElement twoNode1 = xmldoc.CreateElement("CD");
XmlElement twoNodeone123 = xmldoc.CreateElement("TITLE");
twoNodeone123.InnerText = "Empire Burlesque";
twoNode1.AppendChild(twoNodeone123);//添加到CD节点下面
XmlElement twoNodeone11 = xmldoc.CreateElement("ARTIST");
twoNodeone11.InnerText = "Bob Dylan";
twoNode1.AppendChild(twoNodeone11);//添加到CD节点下面
XmlElement twoNodeone22 = xmldoc.CreateElement("COUNTRY");
twoNodeone22.InnerText = "USA";
twoNode1.AppendChild(twoNodeone22);//添加到CD节点下面
XmlElement twoNodeone33 = xmldoc.CreateElement("COMPANY");
twoNodeone33.InnerText = "Columbia";
twoNode1.AppendChild(twoNodeone33);//添加到CD节点下面
XmlElement twoNodeone44 = xmldoc.CreateElement("YEAR");
twoNodeone44.InnerText = "10.90";
twoNode1.AppendChild(twoNodeone44);//添加到CD节点下面
XmlElement twoNodeone45 = xmldoc.CreateElement("PRICE");
twoNodeone45.InnerText = "1985";
twoNode1.AppendChild(twoNodeone45);//添加到CD节点下面
oneNode.AppendChild(twoNode1);//添加到CATALOG节点下面
xmldoc.AppendChild(oneNode);
xmldoc.Save(Server.MapPath("")+"/1.xml");//保存xml
注意:cd节点可以通过循环添加到CATALOG节点下面
展开全部
N级都可以,递归啊!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private void xmlwriter(string startStr,string secondStr,string[] elementStr,string[] elementName)
{
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("phone4.xml"), null);
writer.Formatting = Formatting.Indented; //缩进格式
writer.Indentation = 4;
writer.WriteStartDocument();
writer.WriteStartElement(startStr);
writer.WriteStartElement(secondStr);
int count = 0;
foreach (string str in elementName)
{
writer.WriteStartElement(str);
writer.WriteString(elementStr[count]);
writer.WriteEndElement();
count++;
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
写了个面向过程的函数。。不是好办法,你可以考虑下把xml和对象对应起来创建,这样更灵活。
{
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("phone4.xml"), null);
writer.Formatting = Formatting.Indented; //缩进格式
writer.Indentation = 4;
writer.WriteStartDocument();
writer.WriteStartElement(startStr);
writer.WriteStartElement(secondStr);
int count = 0;
foreach (string str in elementName)
{
writer.WriteStartElement(str);
writer.WriteString(elementStr[count]);
writer.WriteEndElement();
count++;
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
写了个面向过程的函数。。不是好办法,你可以考虑下把xml和对象对应起来创建,这样更灵活。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用DataSet("CATALOG")下面建一个DataTable("CD"),表内建立所需要的DataColumn,把DataRow逐行加入,最后用DataSet类的WriteXml方法写到文件即可。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询