01792.org

Milo

XmlNode to XElement and back again

March 31
by milo 31. March 2010 08:52

 

Constantly having to traverse from Xml to LinqXml so I’ld though I’ld post the static helper classes I constantly use.

public static class XMLHelper
{
public static XElement GetXElement(XmlNode node)
{
XDocument xDoc = new XDocument();
using (XmlWriter xmlWriter = xDoc.CreateWriter())
node.WriteTo(xmlWriter);
return xDoc.Root;
}

public static XmlNode GetXmlNode(XElement element)
{
using (XmlReader xmlReader = element.CreateReader())
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
return xmlDoc;
}
}
}

Tags:

asp.net | C#

Sending SOAP over HttpWebRequest

March 15
by milo 15. March 2010 11:04

Sending SOAP requests over to a SOAP server can sometimes be problematic. (aren’t they always with an internet standard?) Be wary about the ContentLength property. When your sending over to a .net stubbed service you may not need to specify, however, when sending with variable byte length codepage (UTF-8, 16 etc) your length MUST be the byte length, not the un-paged length.

Bad

request.ContentType = "text/xml; charset=utf-8";
request.ContentLength = postData.Length;

Good

request.ContentType = "text/xml; charset=utf-8";
request.ContentLength = System.Text.Encoding.UTF8.GetByteCount(postData);

Otherwise the .net SOAP client will throw a WebException of “Request Cancelled”, when any byte of your request is in the extended range (> ASCII 128), as this is when the UTF-8 variable-ness kicks in, and decides to use more than one byte.
 
UTF-8 can use up to 8 bytes to encode a character within the packet.

Tags:

C# | asp.net | SOAP

DB driven Molonic menu

November 18
by milo 18. November 2009 16:44

I’ve had a few emails this week about a post I made about 3 years ago, driving a DHTML menu from a database.

You can get the demo here

Tags:

C# | Blog | asp.net | Demos

Widget Twitter not found.

Root element is missing.X

About the author

Something about the author

Most comments

RecentComments

Comment RSS