我对c#中的webservices很新。
我将XML发布到具有以下功能的Web服务中:
public static XmlDocument PostXMLTransaction(string URL, XmlDocument XMLDoc)
{
//Declare XMLResponse document
XmlDocument XMLResponse = null;
//Declare an HTTP-specific implementation of the WebRequest class.
HttpWebRequest objHttpWebRequest;
//Declare an HTTP-specific implementation of the WebResponse class
HttpWebResponse objHttpWebResponse = null;
//Declare a generic view of a sequence of bytes
Stream objRequestStream = null;
Stream objResponseStream = null;
//Declare XMLReader
XmlTextReader objXMLReader;
//Creates an HttpWebRequest for the specified URL.
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
try
{
//---------- Start HttpRequest
//Set HttpWebRequest properties
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(XMLDoc.InnerXml);
objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentLength = bytes.Length;
objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
//Get Stream object
objRequestStream = objHttpWebRequest.GetRequestStream();
//Writes a sequence of bytes to the current stream
objRequestStream.Write(bytes, 0, bytes.Length);
//Close stream
objRequestStream.Close();
//---------- End HttpRequest
//Sends the HttpWebRequest, and waits for a response.
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
//---------- Start HttpResponse
if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
//Get response stream
objResponseStream = objHttpWebResponse.GetResponseStream();
//Load response stream into XMLReader
objXMLReader = new XmlTextReader(objResponseStream);
//Declare XMLDocument
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(objXMLReader);
//Set XMLResponse object returned from XMLReader
XMLResponse = xmldoc;
//Close XMLReader
objXMLReader.Close();
}
//Close HttpWebResponse
objHttpWebResponse.Close();
}
catch (WebException we)
{
//TODO: Add custom exception handling
throw new Exception(we.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
//Close connections
objRequestStream.Close();
objResponseStream.Close();
objHttpWebResponse.Close();
//Release objects
objXMLReader = null;
objRequestStream = null;
objResponseStream = null;
objHttpWebResponse = null;
objHttpWebRequest = null;
}
//Return
return XMLResponse;
}
现在,如何将此XML读入url参数指定的Web服务中的XmlDocument。我不确定是否可以将数据发布到webservices,如果我错了就纠正我
我使用此方法将xml发布到aspx页面而不是服务。
答案 0 :(得分:1)
我得到了解决方案
[WebMethod]
public string sendXliff()
{
XmlDocument xmldoc = new XmlDocument();
//if (Request.InputStream != null)
if(HttpContext.Current.Request.InputStream!=null)
{
StreamReader stream = new StreamReader(HttpContext.Current.Request.InputStream);
string xmls = stream.ReadToEnd(); // added to view content of input stream
//XDocument xmlInput = XDocument.Parse(xmls);
xmldoc.LoadXml(xmls);
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, "XSD LOCATION");
settings.ValidationType = ValidationType.Schema;
XmlReader rdr = XmlReader.Create(new StringReader(xmldoc.InnerXml), settings);
while (rdr.Read()) { }
}
try
{
XmlNodeList xmlnode = xmldoc.GetElementsByTagName("ID");
var sid = xmlnode[0].FirstChild.Value;
}
catch (Exception ex)
{
}
return "OK";
}
这将读取输入流,然后将读取的数据转换为XmlDocument。然后我可以解析XML。
答案 1 :(得分:0)
以下是阅读XML文档的简单示例。如果它是单个节点,则使用XmlNode
XmlDocument xmldoc = PostXMLTransaction(string URL, XmlDocument XMLDoc) // call to your code for xml document
XmlNode node= xmldoc.SelectSingleNode("you xml node path here");
if(node != null)
{
var str = node.InnerText;
}
如果是nodelist,请使用XmlNodeList
XmlNodeList nodes = xmlDoc.SelectNodes("you xml node path here");
if(nodes != null)
{
foreach (XmlNode node in nodes)
{
--code here
}
}