新增XML
using System.Xml;
using System.IO;
string filePath= AppDomain.CurrentDomain.BaseDirectory + "so_pe.xml";
XmlDocument xmlDoc = new XmlDocument();
if (!File.Exists(filePath))
{
XmlProcessingInstruction xpi = xmlDoc.CreateProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\" ");
xmlDoc.AppendChild(xpi);
XmlElement xmlEle = xmlDoc.CreateElement("Root");
xmlDoc.AppendChild(xmlEle);
try
{
xmlDoc.Save(filePath);//filePath 文件路径
}
catch (Exception ex)
{
throw ex;
}
}
添加节点
xmlDoc.Load(wuliuTypePath);
XmlNode xmldocSelect = xmlDoc.SelectSingleNode("Root");
XmlElement el = xmlDoc.CreateElement("Config");
el.SetAttribute("TypeCode", "4");
el.SetAttribute("Typename", "销售出库");
XmlElement xesub1 = xmlDoc.CreateElement("pass");
xesub1.InnerText = "123";
el.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("address");
xesub2.InnerText = "昆明";
el.AppendChild(xesub2);
xmldocSelect.AppendChild(el);
xmlDoc.Save(wuliuTypePath);
效果:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Config TypeCode="4" Typename="销售出库">
<pass>123</pass>
<address>昆明</address>
</Config>
</Root>
方法二:
string wuliuTypePath = AppDomain.CurrentDomain.BaseDirectory + "soType.xml"; if (!File.Exists(wuliuTypePath)) {
//创建新的XML
XmlDocument xmlDoc = new XmlDocument();
XmlProcessingInstruction xpi = xmlDoc.CreateProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\" ");
xmlDoc.AppendChild(xpi);
XmlElement xmlEle = xmlDoc.CreateElement("Root");
xmlDoc.AppendChild(xmlEle);
xmlDoc.Save(filePath);
//添加节点
xmlDoc.Load(filePath);
XmlNode xmldocSelect = xmlDoc.SelectSingleNode("Root");
XmlElement Config1 = xmlDoc.CreateElement("Config");
XmlElement xesub11 = xmlDoc.CreateElement("TypeCode");
xesub11.InnerText = "SNDPOR";
Config1.AppendChild(xesub11);
XmlElement xesub12 = xmlDoc.CreateElement("Typename");
xesub12.InnerText = "LS_PI";
Config1.AppendChild(xesub12);
xmldocSelect.AppendChild(Config1);
XmlElement Config2 = xmlDoc.CreateElement("Config");
XmlElement xesub21 = xmlDoc.CreateElement("TypeCode");
xesub21.InnerText = "SNDPRT";
Config2.AppendChild(xesub21);
XmlElement xesub22 = xmlDoc.CreateElement("Typename");
xesub22.InnerText = "KU";
Config2.AppendChild(xesub22);
xmldocSelect.AppendChild(Config2);
XmlElement Config3 = xmlDoc.CreateElement("Config");
XmlElement xesub31 = xmlDoc.CreateElement("TypeCode");
xesub31.InnerText = "SNDPRN";
Config3.AppendChild(xesub31);
XmlElement xesub32 = xmlDoc.CreateElement("Typename");
xesub32.InnerText = "1030";
Config3.AppendChild(xesub32);
xmldocSelect.AppendChild(Config3);
XmlElement Config4 = xmlDoc.CreateElement("Config");
XmlElement xesub41 = xmlDoc.CreateElement("TypeCode");
xesub41.InnerText = "RCVPOR";
Config4.AppendChild(xesub41);
XmlElement xesub42 = xmlDoc.CreateElement("Typename");
xesub42.InnerText = "SAPRED";
Config4.AppendChild(xesub42);
xmldocSelect.AppendChild(Config4);
XmlElement Config5 = xmlDoc.CreateElement("Config");
XmlElement xesub51 = xmlDoc.CreateElement("TypeCode");
xesub51.InnerText = "RCVPRT";
Config5.AppendChild(xesub51);
XmlElement xesub52 = xmlDoc.CreateElement("Typename");
xesub52.InnerText = "KU";
Config5.AppendChild(xesub52);
xmldocSelect.AppendChild(Config5);
XmlElement Config6 = xmlDoc.CreateElement("Config");
XmlElement xesub61 = xmlDoc.CreateElement("TypeCode");
xesub61.InnerText = "RCVPRN";
Config6.AppendChild(xesub61);
XmlElement xesub62 = xmlDoc.CreateElement("Typename");
xesub62.InnerText = "1030";
Config6.AppendChild(xesub62);
xmldocSelect.AppendChild(Config6);
xmlDoc.Save(filePath);
} DataSet ds = new DataSet(); ds.ReadXml(wuliuTypePath); DataTable dt = ds.Tables[0]; this.cmbWuLiuType.ValueMember = "TypeCode"; this.cmbWuLiuType.DisplayMember = "Typename"; this.cmbWuLiuType.DataSource = dt;
浙公网安备 33010602011771号