rakesh
rakesh

Reputation: 612

XML parsing ':' in XElement

I am creating XElement object on name "stream:stream" but it raises XMLException such that ':' cannot be included in a name.

here first stream is a namespace.

Upvotes: 1

Views: 257

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1502406

You specify namespaces like this:

XNamespace streamNs = "some-url-here";
// The + operator here creates an XName
XElement element = new XElement(streamNs + "stream");

To make this create an element of "stream:stream" you'd need an xmlns attribute for stream in some element, e.g.

// Add this to an element - either the element in the namespace, or some parent
// element. The document root is a common place to put all namespaces...
XAttribute streamNs = new XAttribute(XNamespace.Xmlns + "stream",
                                     streamNs.NamespaceName);

Upvotes: 5

Joel Martinez
Joel Martinez

Reputation: 47799

You probably have to make sure you add the namespace to the document before you start using namespaces in the elements:
http://msdn.microsoft.com/en-us/library/bb387075.aspx

Upvotes: 0

Related Questions