生成XML方法:
XmlDocument xml = new XmlDocument();
//插入聲明
XmlDeclaration xmlDelaration = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = xml.DocumentElement;
xml.InsertBefore(xmlDelaration, root);
XmlElement node_Results = xml.CreateElement("Results");
xml.AppendChild(node_Results);
return xml.InnerXml;
或者:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Results></Results>");
XmlNodeList nodeList = xmldoc.GetElementsByTagName("Results");
XmlNode node_results = null;
if (nodeList.Count > 0)
{
node_results = nodeList[0];
}
//屬於Attributes添加方法:
XmlAttribute _ShipLotNo = xml.CreateAttribute("ShipLotNo");
_ShipLotNo.Value = lot.Info.ssl_iShipLotNo.ToString();
node_OrderItem.Attributes.Append(_ShipLotNo);
return xmldoc.InnerXml;
**************************************************************************
讀取XML方法:
private PrePlanReturnData DeserializePrePlanXML(string strXML)
{
PrePlanReturnData rtn = new PrePlanReturnData();
rtn.PEIs = new List<PrePlanReplyPEI>();
rtn.ShipLots = new List<PrePlanReplyShipLot>();
XmlDocument xml = new XmlDocument();
xml.LoadXml(strXML);
XmlNodeList list = xml.GetElementsByTagName("RETURNINFO");
if (list != null)
{
foreach (XmlNode node in list)
{
rtn.ReturnCode = node["RETURNCODE"].InnerText;
rtn.ReturnMessagess = node["RETURNMESSAGE"].InnerText;
break;
}
}
list = xml.GetElementsByTagName("RESULTS");
if (list != null)
{
foreach (XmlNode node in list)
{
if (node.Attributes["ProjectNo"] != null)
{
rtn.OrderNo = node.Attributes["ProjectNo"].Value;
}
if (node.Attributes["ProjectID"] != null)
{
rtn.OrderID = Convert.ToInt32(node.Attributes["ProjectID"].Value);
}
if (rtn.OrderNo == "")
{
if (node.Attributes["SONo"] != null)
{
rtn.OrderNo = node.Attributes["SONo"].Value;
}
if (node.Attributes["SOID"] != null)
{
rtn.OrderID = Convert.ToInt32(node.Attributes["SOID"].Value);
}
}
if (node["ERRORMESSAGE"] != null)
{
rtn.ReturnMessagess += "\n" + node["ERRORMESSAGE"].InnerText;
}
if (node["SUGGESTMESSAGE"] != null)
{
rtn.ReturnMessagess += "\n" + node["SUGGESTMESSAGE"].InnerText;
}
}
}
list = xml.GetElementsByTagName("ShipLot");
if (list != null)
{
foreach (XmlNode node in list)
{
PrePlanReplyShipLot shiplot = new PrePlanReplyShipLot();
if (node.Attributes["ShipLotNo"] != null)
{
shiplot.ShipLotID = Convert.ToInt32(node.Attributes["ShipLotNo"].Value);
}
if (node["SugBeginDate"] != null)
{
shiplot.SugBeginDate = StringToDateTime(node["SugBeginDate"].InnerText);
}
if (node["SugFinishDate"] != null)
{
shiplot.SugFinishDate = StringToDateTime(node["SugFinishDate"].InnerText);
}
if (node["SUGGESTMESSAGE"] != null)
{
shiplot.Message = node["SUGGESTMESSAGE"].InnerText;
}
if (node["ERRORMESSAGE"] != null)
{
shiplot.Message = node["ERRORMESSAGE"].InnerText;
}
rtn.ShipLots.Add(shiplot);
}
}
list = xml.GetElementsByTagName("PEI");
if (list != null)
{
foreach (XmlNode node in list)
{
PrePlanReplyPEI pei = new PrePlanReplyPEI();
if (node.Attributes["ProductID"] != null)
{
pei.ProductID = node.Attributes["ProductID"].Value;
}
if (node.Attributes["EditionCD"] != null)
{
pei.EditionCD = node.Attributes["EditionCD"].Value;
}
if (node.Attributes["ImprintCD"] != null)
{
pei.ImprintCD = node.Attributes["ImprintCD"].Value;
}
if (node["CPMInDate"] != null)
{
pei.CPMInDate = StringToDateTime(node["CPMInDate"].InnerText);
}
rtn.PEIs.Add(pei);
}
}
return rtn;
}