The ':' Character, Hexadecimal Value 0X3A, Cannot Be Included in a Name

The ':' character, hexadecimal value 0x3A, cannot be included in a name

If you want to use namespaces, LINQ to XML makes that really easy:

XNamespace ab = "http://whatever-the-url-is";
XElement tempElement = doc.Descendants(ab + "test").FirstOrDefault();

Look for an xmlns:ab=... section in your document to find out which namespace URI "ab" refers to.

System.Xml.XmlException: The ':' character, hexadecimal value 0x3A, cannot be included in a name

The : is not allowed as part of the name because it's reserved to separate the namespace prefix from the local name. In this context, prefix is a namespace prefix and myVar is the local name.

The prefix is only valid if it is declared in the scope of the element it appears in, and it needs to have an associated namespace - which is omitted in your example, so I'll use http://example.com/ to illustrate:

XNamespace ex = "http://example.com/";

var element = new XElement("foo",
new XAttribute(XNamespace.Xmlns + "prefix", ex),
new XAttribute(ex + "myVar", "value"));

Note the name of the attribute is ex + "myVar". This will create an element like this:

<foo xmlns:prefix="http://example.com/" prefix:myVar="value" />

I've explicitly added the declaration using new XAttribute(XNamespace.Xmlns + "prefix", ex) above, but note this isn't required. If you omit it then one will be generated for you:

XNamespace ex = "http://example.com/";

var element = new XElement("foo",
new XAttribute(ex + "myVar", "value"));

Will result in this:

<foo p1:myVar="value" xmlns:p1="http://example.com/" />

The two outputs are semantically identical.

System.Xml.XmlException: „The ':' character, hexadecimal value 0x3A, cannot be included in a name.”

As @juharr said in the comments, you need to define it as XNamespace first, here's how:

var ns_xsd = XNamespace.Get("http://www.w3.org/2001/XMLSchema");
var ns_msdata = XNamespace.Get("urn:schemas-microsoft-com:xml-msdata");

var root = new XElement("root",
new XElement(ns_xsd + "schema",
new XAttribute("id", "root"),
new XAttribute("xmlns", ""),
new XAttribute(XNamespace.Xmlns + "xsd", ns_xsd),
new XAttribute(XNamespace.Xmlns + "msdata", ns_msdata),
new XAttribute(ns_msdata + "IsDataSet", "true")));

'The ':' character, hexadecimal value 0x3A, cannot be included in a name , when loading xml file usinf XDocument

The correct processing-instruction name is xml-stylesheet, not xml:stylesheet. Colons in processing instruction names are not allowed in well-formed XML - or to put it another way, you are trying to read a file that is not XML.

The ':' character, hexadecimal value 0x3A, cannot be included in a name

The following code

    static void Main(string[] args)
{

var XMLFeed = XDocument.Parse(
@"<rss>
<channel>

....items....

<item>
<title>Pentagon confirms plan to create new spy agency</title>
<link>http://feeds.foxnews.com/~r/foxnews/most-popular/~3/lVUZwCdjVsc/</link>
<category>politics</category>
<dc:creator xmlns:dc='http://purl.org/dc/elements/1.1/' />
<pubDate>Tue, 24 Apr 2012 12:44:51 PDT</pubDate>
<guid isPermaLink='false'>http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</guid>
<content:encoded xmlns:content='http://purl.org/rss/1.0/modules/content/'><![CDATA[|http://global.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg<img src='http://feeds.feedburner.com/~r/foxnews/most-popular/~4/lVUZwCdjVsc' height='1' width='1'/>]]></content:encoded>
<description>The Pentagon confirmed Tuesday that it is carving out a brand new spy agency expected to include several hundred officers focused on intelligence gathering around the world.&amp;#160;</description>
<dc:date xmlns:dc='http://purl.org/dc/elements/1.1/'>2012-04-4T19:44:51Z</dc:date>
<!-- <feedburner:origLink>http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</feedburner:origLink> -->
</item>

....items....

</channel>
</rss>");
XNamespace contentNs = "http://purl.org/rss/1.0/modules/content/";
var feeds = from feed in XMLFeed.Descendants("item")
select new
{
Title = (string)feed.Element("title"),
Link = (string)feed.Element("link"),
pubDate = (string)feed.Element("pubDate"),
Description = (string)feed.Element("description"),
MediaContent = GetMediaContent((string)feed.Element(contentNs + "encoded"))
};
foreach(var item in feeds)
{
Console.WriteLine(item);
}
}

private static string GetMediaContent(string content)
{
int imgStartPos = content.IndexOf("<img");
if(imgStartPos > 0)
{
int startPos = content[0] == '|' ? 1 : 0;

return content.Substring(startPos, imgStartPos - startPos);
}

return string.Empty;
}

results in:


{ Title = Pentagon confirms plan to create new spy agency, Link = http://feeds.f
oxnews.com/~r/foxnews/most-popular/~3/lVUZwCdjVsc/, pubDate = Tue, 24 Apr 2012 1
2:44:51 PDT, Description = The Pentagon confirmed Tuesday that it is carving out
a brand new spy agency expected to include several hundred officers focused on
intelligence gathering around the world. , MediaContent = http://global
.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg }
Press any key to continue . . .

A few points:

  • You never want to treat Xml as text - in your case you removed the namespace declaration but actually if the namespace was declared inline (i.e. without binding to the prefix) or a different prefix would be defined your code would not work even though semantically both documents would be equivalent
  • Unless you know what's inside CDATA and how to treat it you always want to treat is as text. If you know it's something else you can treat it differently after parsing - see my elaborate on CDATA below for more details
  • To avoid NullReferenceExceptions if the element is missing I used explicit conversion operator (string) instead of invoking .Value
  • the Xml you posted was not a valid xml - there was missing namespace Uri for feedburner prefix

This is no longer related to the problem but may be helpful for some folks so I am leaving it

As far as the contents of the encode element is considered it is inside CDATA section. What's inside CDATA section is not an Xml but plain text. CDATA is usually used to not have to encode '<', '>', '&' characters (without CDATA they would have to be encoded as < > and & to not break the Xml document itself) but the Xml processor treat characters in the CDATA as if they were encoded (or to be more correct in encodes them). The CDATA is convenient if you want to embed html because textually the embedded content looks like the original yet it won't break your xml if the html is not a well-formed Xml. Since the CDATA content is not an Xml but text it is not possible to treat it as Xml. You will probably need to treat is as text and use for instance regular expressions. If you know it is a valid Xml you can load the contents to an XElement again and process it. In your case you have got mixed content so it is not easy to do unless you use a little dirty hack. Everything would be easy if you have just one top level element instead of mixed content. The hack is to add the element to avoid all the hassle. Inside the foreach look you can do something like this:

var mediaContentXml = XElement.Parse("<content>" + (string)item.MediaContent + "</content>");
Console.WriteLine((string)mediaContentXml.Element("img").Attribute("src"));

Again it's not pretty and it is a hack but it will work if the content of the encoded element is valid Xml. The more correct way of doing this is to us XmlReader with ConformanceLevel set to Fragment and recognize all kinds of nodes appropriately to create a corresponding Linq to Xml node.

Hexadecimal values cannot be included in a name vb.net

Here is an example, with just the <media:content.../> element:

Dim FilePath As String
FilePath = "C:\Temp\myfile.xml"
Dim document As New XDocument
If File.Exists(FilePath) Then
document = XDocument.Load(FilePath)
Else
Console.WriteLine("File is missing")
Exit Sub
End If

' Example: Write
' <item>
' <media:content url="http://any link" height="266" width="127" />
' </item>

Dim ns As XNamespace = "http://that.is.my.namespace"
Dim newItem = New XElement( "item",
New XElement(ns + "content",
New XAttribute("url", "http://any link"),
New XAttribute("height", 266),
New XAttribute("width", 127)))

document.Root.Add(newItem)
document.Save(FilePath)

Console.WriteLine("Done")

Please note that you must adapt this line:

Dim ns As XNamespace = "http://that.is.my.namespace"

Replace the string by whatever namespace media has in your XML file.
E.g. if your XML file contains something like

<items xmlns:media="http://other.namespace">

then change the above line to

Dim ns As XNamespace = "http://other.namespace"

XmlWriter The ':' character, hexadecimal value 0x3A, cannot be included in a name

You need to use the overload of WriteStartElement that takes two parameters:

writer.WriteStartElement("news", "http://www.google.com/schemas/sitemap-news/0.9");
// Tag ----^ ^--- Namespace


Related Topics



Leave a reply



Submit