C # XML processing in WebServices,
1. During enterprise system integration, a large number of fields are difficult to process, and can be quickly processed using Format.
String SQL = @ "SELECT * FROM table WHERE TASKID = '" + taskid + "'";
DataTable dt = DBUtil. Query (SQL). Tables [0];
// Here is the path for obtaining the XML template
Stream xmlStream = Assembly. GetExecutingAssembly (). GetManifestResourceStream ("BPMWinService. Xml." + xmlname + ". xml ");
XmlDocument xml = new XmlDocument ();
Xml. Load (xmlStream );
System. Xml. XmlNamespaceManager ns = new XmlNamespaceManager (xml. NameTable );
Ns. AddNamespace ("soap", "http: // address ");
Ns. AddNamespace ("NS2." http: // address /");
XmlNode InputParameters = xml. SelectSingleNode ("soap: Envelope/soap: Body/int32: InputParameters", ns );
String mModel = InputParameters. InnerXml;
InputParameters. RemoveAll ();
// Use Format to set the InnerText value. Note the meaning and usage of InnerXml, InnerText, and OuterXml.
// Format is more meaningful in the loop, but you need to set the XML template first.
MModel = string. format (mModel, Convert. toString (dt. rows [0] ["invoice_id"]), Convert. toInt32 (dt. rows [0] ["taskid"]), state );
InputParameters. InnerXml = mModel;
// System. Diagnostics. Stopwatch stopwatch = new System. Diagnostics. Stopwatch ();
// Stopwatch. Start ();
WebServiceCaller wsCaller = new WebServiceCaller ();
String isSaveXML = System. Configuration. ConfigurationManager. receivettings ["IsSaveXML"];
If (isSaveXML = "true ")
Xml. Save (@ "C:/BPMWinService/ERP_BPMFP_XML.xml ");
// Call others' interfaces by yourself
String strReturnValue = wsCaller. QuerySoapWebService (url, YZHelper. ERP_BPMFP, xml );
// Stopwatch. Stop ();
XmlDocument returnDoc = new XmlDocument ();
ReturnDoc. LoadXml (strReturnValue );
// I don't know why I can't get the node here. If I don't fully understand it, I will use the following method to get the node list and then use a loop to get the node value. The number of LastChild is related to the returned XML level, write at several layers
// Several LastChild
XmlNodeList XNL = returnDoc. LastChild. ChildNodes;
XmlNode _ Message = null;
String massage = string. Empty;
For (int I = 0; I <XNL. Count; I ++)
{
If (Convert. ToString (XNL [I]. Name) = "***")
{
_ Message = XNL [I];
If (_ Message. InnerText = "E ")
{
For (int j = 0; j <XNL. Count; j ++)
{
If (Convert. ToString (XNL [j]. Name) = "***")
{
Massage = XNL [j]. InnerText;
Break;
}
}
}
Break;
}
}
/// <Summary>
/// Call webservice through the SOAP protocol
/// </Summary>
Private string QuerySoapWebService (String url, String methodName, string xmlNs, XmlDocument xml)
{
Try
{
XML_NAMESPACE [url] = xmlNs;
Byte [] data;
HttpWebRequest request = (HttpWebRequest) HttpWebRequest. Create (url );
//???? Head
Request. Method = "POST ";
Request. ContentType = "text/xml; charsets = UTF-8 ";
Request. AutomaticDecompression = DecompressionMethods. GZip;
String username = YZHelper. UserName;
String password = YZHelper. PassWord;
Request. Credentials = new NetworkCredential (username, password );
Using (MemoryStream MS = new MemoryStream ())
{
XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. LoadXml (xml. InnerXml. ToString ());
XmlDoc. Save (MS );
Data = ms. ToArray ();
}
////? Soap ????????
WriteRequestData (request, data );
String retXml = string. Empty;
HttpWebResponse myrespones;
Try
{
Myrespones = (HttpWebResponse) request. GetResponse ();
}
Catch (WebException ex)
{
Myrespones = (HttpWebResponse) ex. Response;
}
RetXml = ReadMotmResponse (myrespones );//?? Webservice ????
//??????
If (request! = Null)
{
Request. Abort ();
Request = null;
}
If (myrespones! = Null)
{
Myrespones. Close ();
Myrespones = null;
}
Return retXml;
}
Catch (Exception e)
{
Return e. Message;
}
}