<?xml version="1.0" encoding="utf-8" ?>
<pricelist>
<oilprices>
<price year="1" value="100" />
<price year="2" value="200" />
<price year="3" value="300" />
<price year="4" value="121" />
<price year="5" value="200" />
</oilprices>
<fuelprices>
<price year="1" e95="122" e98="122" e99="121"/>
<price year="2" e95="123" e98="132" e99="112"/>
<price year="3" e95="121" e98="112" e99="112"/>
<price year="4" e95="122" e98="121" e99="122"/>
<price year="5" e95="142" e98="112" e99="121"/>
</fuelprices>
</pricelist>
<pricelist>
<oilprices>
<price year="1" value="100" />
<price year="2" value="200" />
<price year="3" value="300" />
<price year="4" value="121" />
<price year="5" value="200" />
</oilprices>
<fuelprices>
<price year="1" e95="122" e98="122" e99="121"/>
<price year="2" e95="123" e98="132" e99="112"/>
<price year="3" e95="121" e98="112" e99="112"/>
<price year="4" e95="122" e98="121" e99="122"/>
<price year="5" e95="142" e98="112" e99="121"/>
</fuelprices>
</pricelist>
/// 在根节点下添加父节点
/// </summary>
public static void AddParentNode(string xmlPath,string parentNode)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
XmlElement Node = xdoc.CreateElement(parentNode);
xdoc.DocumentElement.PrependChild(Node);
xdoc.Save(xmlPath);
}
/// <summary>
/// 插入一节点,带一属性
/// </summary>
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib, AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
/// <summary>
/// 向一个节点添加属性
/// </summary>
public static void AddAttribute(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlAttribute nodeAttribute = objXmlDoc.CreateAttribute(NodeAttribute1);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
nodePath.Attributes.Append(nodeAttribute);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(NodeAttribute1, NodeAttributeText);
objXmlDoc.Save(xmlPath);
}
XmlNode n = doc.CreateElement("pricelist");
doc.AppendChild(n);
XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(xmldecl, n);
//XmlNode n1 = ("oilprices");
//doc.InsertBefore(n1, n);
//n = doc.CreateElement("oilprices");
XmlElement xe1 = doc.CreateElement("price");
xe1.SetAttribute("year", "1");//设置该节点year属性
xe1.SetAttribute("value", "212");//设置该节点value属性 doc.DocumentElement.AppendChild(xe1);
doc.Save(Server.MapPath("App_Data\\" + "aaa.xml"));-----------------------------------------------------------
生成的是如下格式的
<?xml version="1.0" encoding="utf-8" ?>
- <pricelist>
<price year="1" value="212" />
</pricelist>那个代码在那里怎么改造一下!~谢谢
对XML不太熟悉!
{ public static void CreateXML()
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),new XElement("pricelist"));
XElement root = new XElement("pricelist");
XElement node1 = new XElement("oilprices",
new XElement("price",
new XAttribute("year", "1"),new XAttribute("value","100")),
new XElement("price",
new XAttribute("year","2"),new XAttribute("value","200")),
new XElement("price",
new XAttribute("year","3"),new XAttribute("value","300")),
new XElement("price",
new XAttribute("year","4"),new XAttribute("value","121")),
new XElement("price",
new XAttribute("year","5"),new XAttribute("value","200")));
XElement node2 = new XElement("fuelprices",
new XElement("price",
new XAttribute("e95", "1"), new XAttribute("e98", "122"),new XAttribute("e99","121"),
new XElement("price",
new XAttribute("year", "2"), new XAttribute("value", "123"),new XAttribute("e99","112")),
new XElement("price",
new XAttribute("year", "3"), new XAttribute("value", "121"),new XAttribute("e99","112")),
new XElement("price",
new XAttribute("year", "4"), new XAttribute("value", "122"),new XAttribute("e99","122")),
new XElement("price",
new XAttribute("year", "5"), new XAttribute("value", "142"),new XAttribute("e99","121"))));
root.Add(node1);
root.Add(node2);
root.Save("demo.xml");
//Console.WriteLine(root);
Console.Read();
}
}添加using System.Xml.Linq;命名空间
using System;using System.Xml.Linq; public class LinqXml
{ public static void CreateXML()
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"), new XElement("pricelist",""));
XElement root = new XElement("pricelist");
XElement node1 = new XElement("oilprices",
new XElement("price",
new XAttribute("year", "1"), new XAttribute("value", "100")),
new XElement("price",
new XAttribute("year", "2"), new XAttribute("value", "200")),
new XElement("price",
new XAttribute("year", "3"), new XAttribute("value", "300")),
new XElement("price",
new XAttribute("year", "4"), new XAttribute("value", "121")),
new XElement("price",
new XAttribute("year", "5"), new XAttribute("value", "200")));
XElement node2 = new XElement("fuelprices",
new XElement("price",
new XAttribute("e95", "1"), new XAttribute("e98", "122"), new XAttribute("e99", "121"),
new XElement("price",
new XAttribute("e95", "2"), new XAttribute("e98", "123"), new XAttribute("e99", "112")),
new XElement("price",
new XAttribute("e95", "3"), new XAttribute("e98", "121"), new XAttribute("e99", "112")),
new XElement("price",
new XAttribute("e95", "4"), new XAttribute("e98", "122"), new XAttribute("e99", "122")),
new XElement("price",
new XAttribute("e95", "5"), new XAttribute("e98", "142"), new XAttribute("e99", "121"))));
root.Add(node1);
root.Add(node2);
root.Save("demo.xml");
Console.WriteLine(root);
Console.Read();
}
}
错误 1 命名空间“System.Xml”中不存在类型或命名空间名称“Linq”(是缺少程序集引用吗?) using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Linq;/// <summary>
/// Class1 的摘要说明
/// </summary>public class LinqXml
{ public static void CreateXML()
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"), new XElement("pricelist", ""));
XElement root = new XElement("pricelist");
XElement node1 = new XElement("oilprices",
new XElement("price",
new XAttribute("year", "1"), new XAttribute("value", "100")),
new XElement("price",
new XAttribute("year", "2"), new XAttribute("value", "200")),
new XElement("price",
new XAttribute("year", "3"), new XAttribute("value", "300")),
new XElement("price",
new XAttribute("year", "4"), new XAttribute("value", "121")),
new XElement("price",
new XAttribute("year", "5"), new XAttribute("value", "200")));
XElement node2 = new XElement("fuelprices",
new XElement("price",
new XAttribute("e95", "1"), new XAttribute("e98", "122"), new XAttribute("e99", "121"),
new XElement("price",
new XAttribute("e95", "2"), new XAttribute("e98", "123"), new XAttribute("e99", "112")),
new XElement("price",
new XAttribute("e95", "3"), new XAttribute("e98", "121"), new XAttribute("e99", "112")),
new XElement("price",
new XAttribute("e95", "4"), new XAttribute("e98", "122"), new XAttribute("e99", "122")),
new XElement("price",
new XAttribute("e95", "5"), new XAttribute("e98", "142"), new XAttribute("e99", "121"))));
root.Add(node1);
root.Add(node2);
root.Save("demo.xml");
Console.WriteLine(root);
Console.Read();
}
}
XmlNode xmlnode;
XmlElement xmlelem; xmldoc = new XmlDocument();
//加入XML的声明段落
xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);
//加入一个根元素
xmlelem = xmldoc.CreateElement("", "pricelist", "");
xmldoc.AppendChild(xmlelem);
//加入另外一个元素
XmlNode root = xmldoc.SelectSingleNode("pricelist");//查找<Employees> XmlElement xe1 = xmldoc.CreateElement("oilprices");//创建一个<Node>节点 for (int k = 0; k < dt.Rows.Count; k++)
{
XmlElement temp = xmldoc.CreateElement("price");
temp.SetAttribute("year", dt.Rows[k]["b"].ToString());//设置该节点year属性
temp.SetAttribute("value", dt.Rows[k]["c"].ToString());//设置该节点value属性
xe1.AppendChild(temp);
}
root.AppendChild(xe1);
XmlElement xe2 = xmldoc.CreateElement("fuelprices");//创建一个<Node>节点
for (int k = 0; k < dt.Rows.Count; k++)
{
XmlElement temp = xmldoc.CreateElement("price");
temp.SetAttribute("year", dt.Rows[k]["b"].ToString());//设置该节点year属性
temp.SetAttribute("A1", dt.Rows[k]["c"].ToString());//设置该节点A1属性
temp.SetAttribute("A2", dt.Rows[k]["d"].ToString());//设置该节点A2属性
temp.SetAttribute("A3", dt.Rows[k]["e"].ToString());//设置该节点A3属性
temp.SetAttribute("A4", dt.Rows[k]["f"].ToString());//设置该节点A4属性
xe2.AppendChild(temp);
}
root.AppendChild(xe2);//添加到<Employees>节点中
//保存创建好的XML文档
xmldoc.Save(Server.MapPath("App_Data\\" + "test.xml"));
-----------------------------------------------------------------------------------------------------
输出结果:
<?xml version="1.0"?>
<pricelist>
<oilprices>
<price year="1" value="22" />
<price year="2" value="32" />
<price year="3" value="32" />
<price year="4" value="32" />
<price year="5" value="21" />
</oilprices>
<fuelprices>
<price year="1" A1="22" A2="12" A3="21" A4="12" />
<price year="2" A1="32" A2="32" A3="65" A4="21" />
<price year="3" A1="32" A2="54" A3="22" A4="98" />
<price year="4" A1="32" A2="21" A3="67" A4="4" />
<price year="5" A1="21" A2="32" A3="54" A4="43" />
</fuelprices>
</pricelist>感谢各位支持!~
查看目录->.NET开发->.NET Framework SDK->类库参考.
下面找相关的类帮助文档. XML命名空间说的详细,有例子.详细明了..
using System.Xml;
using System.Collections.Generic;
using System.IO;
class test
{
static XmlDocument doc;
static string file = @"f:\test\demo.xml";
static string attr1;
static string attr2;
static string attr3;
static string attr4;
static string attr5;
static List<string> v1=new List<string> ();
static List<string> v2=new List<string> ();
static List<string> v3=new List<string> ();
static List<string> v4=new List<string> ();
static void Main()
{
FileStream fs = File.Create(file);
fs.Close();
doc = createxmldocument(file, "pricelist");
dataprepare ();
XmlNode rootnode = doc.SelectSingleNode("pricelist");
rootnode.RemoveAll();
XmlInsertNode(doc, "pricelist", "oilprices");
XmlInsertNode(doc, "pricelist", "fuelprices");
XmlInsertNode(doc, "pricelist/oilprices", "price");
for (int i = 1; i <= 5;i++ )
{
XmlInsertNodeAttri1(doc, "pricelist/oilprices", "price",attr1 , i.ToString (),attr2, v1 [i-1]);
XmlInsertNodeAttri2(doc, "pricelist/fuelprices", "price", attr1 , i.ToString (),attr3, v2 [i-1],attr4, v3[i-1],attr5, v4[i-1]);
}
doc.Save(file);
string con = rootnode.OuterXml;
Console.WriteLine(con);
}
public static XmlDocument createxmldocument()
{
XmlDocument doc = new XmlDocument();
XmlNode n = doc.CreateElement("pricelist");
doc.AppendChild(n);
XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(xmldecl, n);
return doc;
}
/// <summary>
/// 创建xml文件
/// </summary
public static XmlDocument createxmldocument(string xmlPath, string parentNode)
{
XmlDocument doc = new XmlDocument();
XmlNode n = doc.CreateElement("pricelist");
doc.AppendChild(n);
XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(xmldecl, n);
return doc;
}
/// <summary>
/// 插入不带属性的节点
/// </summary>
public static XmlDocument XmlInsertNode(XmlDocument doc, string MainNode, string Node)
{
XmlNode objNode = doc.SelectSingleNode(MainNode);
XmlNode xn = doc.CreateElement(Node);
// XmlText text = doc.CreateTextNode("");
objNode.AppendChild(xn);
// objNode.LastChild.AppendChild(text);
return doc;
}
/// <summary>
/// 插入带属性的节点
/// </summary>
public static XmlDocument XmlInsertNodeAttri1(XmlDocument doc, string MainNode, string Node, string NodeAttribute1, string NodeAttributeText1, string NodeAttribute2, string NodeAttributeText2)
{
XmlNode objNode = doc.SelectSingleNode(MainNode);
XmlNode xn = doc.CreateElement(Node);
XmlNode attr1 = doc.CreateNode(XmlNodeType.Attribute, NodeAttribute1, "");
attr1.Value = NodeAttributeText2;
xn.Attributes.SetNamedItem(attr1);
XmlNode attr2 = doc.CreateNode(XmlNodeType.Attribute, NodeAttribute2, "");
attr2.Value = NodeAttributeText2;
xn.Attributes.SetNamedItem(attr2);
objNode.AppendChild(xn);
return doc;
}
public static XmlDocument XmlInsertNodeAttri2(XmlDocument doc, string MainNode, string Node, string NodeAttribute1, string NodeAttributeText1, string NodeAttribute2, string NodeAttributeText2, string NodeAttribute3, string NodeAttributeText3, string NodeAttribute4, string NodeAttributeText4)
{
XmlNode objNode = doc.SelectSingleNode(MainNode);
XmlNode xn = doc.CreateElement(Node);
XmlNode attr1 = doc.CreateNode(XmlNodeType.Attribute, NodeAttribute1, "");
attr1.Value = NodeAttributeText2;
xn.Attributes.SetNamedItem(attr1);
XmlNode attr2 = doc.CreateNode(XmlNodeType.Attribute, NodeAttribute2, "");
attr2.Value = NodeAttributeText2;
xn.Attributes.SetNamedItem(attr2);
XmlNode attr3 = doc.CreateNode(XmlNodeType.Attribute, NodeAttribute3, "");
attr3.Value = NodeAttributeText3;
xn.Attributes.SetNamedItem(attr3);
XmlNode attr4 = doc.CreateNode(XmlNodeType.Attribute, NodeAttribute4, "");
attr4.Value = NodeAttributeText4;
xn.Attributes.SetNamedItem(attr4);
objNode.AppendChild(xn);
return doc;
}
static void dataprepare()
{
attr1 = "year";
attr2 = "Value";
attr3 = "e95";
attr4 = "e98";
attr5 = "e99";
v1.Add("100");
v1.Add("200");
v1.Add("300");
v1.Add("121");
v1.Add("200");
v2.Add("122");
v2.Add("123");
v2.Add("121");
v2.Add("122");
v2.Add("142");
v3.Add("122");
v3.Add("132");
v3.Add("112");
v3.Add("121");
v3.Add("112");
v4.Add("121");
v4.Add("112");
v4.Add("112");
v4.Add("122");
v4.Add("121"); }
}