The following methods are used in the code samples throughout this reference.
This method displays the return codes and XML body of a message. DisplayMethod is used by all methods to display the returned messages.
using System.Xml; using System.Xml.XPath;
public static void DisplayMessage(HttpWebResponse response) { //Write out the status code number and name Console.WriteLine((int)response.StatusCode + " " + response.StatusCode);
//stream the body of the message into an XML reader. XmlTextReader reader = new XmlTextReader(response.GetResponseStream()); reader.WhitespaceHandling = WhitespaceHandling.None;
//try to load the XML into an XML doc and display it. XmlDocument xd = new XmlDocument(); try { xd.Load(reader); XmlNode xnodDE = xd.DocumentElement;
ChildDisplay(xnodDE, 0); reader.Close(); } //if anything goes wrong, return. catch { return; } }
private static void ChildDisplay(XmlNode xnod, int level) { XmlNode xnodWorking; String pad = new String(' ', level * 2);
Console.WriteLine(pad + xnod.Name + "(" + xnod.NodeType.ToString() + ": " + xnod.Value + ")");
if (xnod.NodeType == XmlNodeType.Element) { XmlNamedNodeMap mapAttributes = xnod.Attributes; for (int i = 0; i < mapAttributes.Count; i++) { Console.WriteLine(pad + " " + mapAttributes.Item(i).Name + " = " + mapAttributes.Item(i).Value); } }
if (xnod.HasChildNodes) { xnodWorking = xnod.FirstChild; while (xnodWorking != null) { ChildDisplay(xnodWorking, level + 1); xnodWorking = xnodWorking.NextSibling; } } } |