日期:2014-05-19  浏览次数:20806 次

如何将XML文件转化为字符串,如何将字符串转化为XML文件
如题,谢谢!

------解决方案--------------------
将文件读出来就成了字符串,将字符串保存起来就成了文件 活活
------解决方案--------------------
//XML String
string strXML = @ " <?xml version= '1.0 ' encoding= 'UTF-8 '?> " +
" <SCSShippingInfo> " +
" <IsSuccess> Y </IsSuccess> " +
" <Message> Success! </Message> " +
" <ErpData> " +
" <INVOICE_NO> l9128565 </INVOICE_NO> " +
" <SCS_QTY> 96 </SCS_QTY> " +
" <ORG> M01 </ORG> " +
" <STATUS> NULL </STATUS> " +
" <OQC_CHECK_TIME> NULL </OQC_CHECK_TIME> " +
" <SHIPPING_TIME> 2007-03-07 18:10:38.0 </SHIPPING_TIME> " +
" </ErpData> " +
" </SCSShippingInfo> ";

//Load Xml
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strXML);
------解决方案--------------------
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.LoadXml(content);//content是XML字符串
}
catch (Exception e)
{
throw new AppException( "不是XML文档格式或是XML语法错误 ", e);
}
XmlNode rootNode = xmlDoc.DocumentElement;
if (rootNode.Name.ToLower() != "documentinfo ")
{
return SQLAccessState.Fault; //不是公文信息
}
try
{
XmlNode archiveInfoNode = rootNode.SelectSingleNode( "DocInfo ");//用XPath表达示取得节点值
string date = archiveInfoNode.SelectSingleNode( "PublicDate ").InnerText.ToString();
if (date == " " || date == null)
{
date = System.DateTime.Today.ToString();
}
archiveInfo.DocNo = archiveInfoNode.SelectSingleNode( "DocNo ").InnerText.ToString();
archiveInfo.Title = archiveInfoNode.SelectSingleNode( "Title ").InnerText.ToString();
archiveInfo.SendUnitCode = archiveInfoNode.SelectSingleNode( "SendUnitCode ").InnerText.ToString();
archiveInfo.PublicDate = Convert.ToDateTime(date);
archiveInfo.Urgent = archiveInfoNode.SelectSingleNode( "Urgent ").InnerText.ToString();
archiveInfo.Secret = archiveInfoNode.SelectSingleNode( "Secret ").InnerText.ToString();
archiveInfo.Subject = archiveInfoNode.SelectSingleNode( "Subject ").InnerText.ToString();
archiveInfo.KeyWord = archiveInfoNode.SelectSingleNode( "KeyWord ").InnerText.ToString();
archiveInfo.Drafter = archiveInfoNode.SelectSingleNode( "Drafter ").InnerText.ToString();
archiveInfo.Dutyman = archiveInfoNode.SelectSingleNode( "Dutyman ").InnerText.ToString();
archiveInfo.Remark = archiveInfoNode.SelectSingleNode( "Remark ").InnerText.ToString();
archiveInfo.IsDeleted = Convert.ToInt32(archiveInfoNode.SelectSingleNode( "Status ").InnerText.ToString());
}
catch(Exception e)
{
throw new AppException( "XML文档中公文信息节点不正确 ", e);
}
------解决方案--------------------