Generate XML with multiple namespaces using XDocument - c#

I have XML like this:
<stream:stream to="lap-020.abcd.co.in" from="sourav#lap-020.abcd.co.in" xml:lang="en" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0"/>
Try to generate the XML using XDocument like this
private readonly XNamespace _streamNamespace = "http://etherx.jabber.org/streams";
private readonly XName _stream;
_stream = _streamNamespace + "stream";
XDocument xdoc=new XDocument(
new XElement(_stream,
new XAttribute("from", "sourav#lap-020.abcd.co.in"),
new XAttribute("to","lap-020.abcd.co.in"),
new XAttribute("xmlns:stream","http://etherx.jabber.org/streams"),
new XAttribute("version","1.0"),
new XAttribute("xml:lang","en")
));
But I get an exception:
Additional information: The ':' character, hexadecimal value 0x3A, cannot be included in a name.

To add namespace declaration you can use XNamespace.Xmlns, and to reference the predefined namespace prefix xml use XNamespace.Xml, for example :
XNamespace stream = "http://etherx.jabber.org/streams";
var result = new XElement(stream + "stream",
new XAttribute("from", "sourav#lap-020.abcd.co.in"),
new XAttribute("to","lap-020.abcd.co.in"),
new XAttribute(XNamespace.Xmlns + "stream", stream),
new XAttribute("version","1.0"),
new XAttribute(XNamespace.Xml+"lang","en"),
String.Empty);
Console.WriteLine(result);
//above prints :
//<stream:stream from="sourav#lap-020.abcd.co.in" to="lap-020.abcd.co.in"
// xmlns:stream="http://etherx.jabber.org/streams" version="1.0"
// xml:lang="en">
//</stream:stream>

you can add the namespace like
XElement root = new XElement("{http://www.adventure-works.com}Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement("{http://www.adventure-works.com}Child", "child content")
);
This example produces the following output:
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>

Related

Error when redefining a different xml Namespace to an existing alias using XElement with System.Xml.Linq

I try programming the following xml document (simplified) as code:
<P xmlns="http://schemas.microsoft.com/x/2010/manifest"
xmlns:ab="http://schemas.microsoft.com/a/2010/manifest"
xmlns:ac="http://schemas.microsoft.com/a/2013/manifest">
<ab:Ex xmlns:ab="http://schemas.microsoft.com/a/2013/manifest">
</ab:Ex>
</P>
Now an element redefines the namespace:
xmlns:ab="http://schemas.microsoft.com/a/2013/manifest".
how do I get the element into my program? It is obviously valid because I can load the xml file like this:
Add(new XAttribute (XNamespace.Xmlns + "ab")
I get the error message:
System.Xml.XmlException: 'The prefix 'ab' cannot be redefined from 'http://schemas.microsoft.com/a/2010/manifest' to 'http://schemas.microsoft.com/a/2013/manifest' within the same start element tag.'
Actually logical (redefinition) but when I load the document, the element is accepted.
using System.Xml.Linq;
namespace xmltest
{
class Program
{
static void Main(string[] args)
{
//Load
XDocument doc1 = XDocument.Load(#"c:\simple.xml", LoadOptions.None);
System.Console.WriteLine(doc1.ToString());
//Create new
XNamespace ab = "http://schemas.microsoft.com/a/2010/manifest";
XNamespace nsx = "http://schemas.microsoft.com/x/2010/manifest";
XDocument doc2 = new XDocument(new XDeclaration("1.0", "utf-8", ""),
new XElement(nsx + "P"));
doc2.Element(nsx + "P").Add(new XAttribute("xmlns",
"http://schemas.microsoft.com/x/2010/manifest"));
doc2.Element(nsx + "P").Add(new XAttribute(XNamespace.Xmlns + "ab",
"http://schemas.microsoft.com/a/2010/manifest"));
doc2.Element(nsx + "P").Add(new XAttribute(XNamespace.Xmlns + "ac",
"http://schemas.microsoft.com/a/2013/manifest"));
XElement xml = new XElement(ab + "Ex");
//--> Below does not work work
xml.Add(new XAttribute(XNamespace.Xmlns + "ab",
"http://schemas.microsoft.com/a/2013/manifest"));
doc2.Element(nsx + "P").Add(xml);
System.Console.WriteLine(doc2.ToString());
}
}
}
Anybody have any idea how to solve this problem?
In theory, because
<ab:Ex xmlns:ab="http://schemas.microsoft.com/a/2013/manifest">
actually means that Ex is to be defined in the new namespace (2013), you can get what you want by defining Ex in the new namespace, i.e. something like:
XNamespace abOld = "http://schemas.microsoft.com/a/2010/manifest";
XNamespace abNew = "http://schemas.microsoft.com/a/2013/manifest";
XNamespace nsx = ...
XElement xml = new XElement(abNew + "Ex");
xml.Add(new XAttribute(XNamespace.Xmlns + "ab",
"http://schemas.microsoft.com/a/2013/manifest"));
However, repurposing the alias of an existing namespace just so that you can use the same alias is going to be really confusing to future maintenance - as the alias leaves scope, it will revert to it's old definition. I would recommend that you at least create different, unique aliases for the 2010 and 2013 namespaces.
Also, unless the intended consumer of the Xml was buggy, and required specific aliases or namespace scopes, I would generally avoid micro-managing the namespace aliasing and namespace scoping - let Linq to Xml manage that for you.
For instance, how about just:
XNamespace abOld = "http://schemas.microsoft.com/a/2010/manifest";
XNamespace abNew = "http://schemas.microsoft.com/a/2013/manifest";
// Build up the document by providing element, attribute + applicable namespaces:
var root = new XElement(abOld + "P",
new XElement(abNew + "Ex"));

XDocument Error Name cannot begin with the '<' character, hexadecimal value 0x3C

I am trying to create xml file using XDcoument, but I am getting following error
Name cannot begin with the '<' character, hexadecimal value 0x3C
here is my code
XDocument d = new XDocument(
new XElement("<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>",
new XElement("<S:Header xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>",
new XElement("<ns13:ACASecurityHeader xmlns='urn:us:gov:treasury:irs:ext:aca:air:7.0' xmlns:ns10='urn:us:gov:treasury:irs:msg:acauibusinessheader' xmlns:ns11='http://www.w3.org/2000/09/xmldsig#' xmlns:ns12='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' xmlns:ns13='urn:us:gov:treasury:irs:msg:acasecurityheader' xmlns:ns2='urn:us:gov:treasury:irs:common' xmlns:ns3='urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage' xmlns:ns4='urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage' xmlns:ns5='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' xmlns:ns6='urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage' xmlns:ns7='urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage' xmlns:ns8='urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter' xmlns:ns9='urn:us:gov:treasury:irs:msg:acabusinessheader'>"),
new XElement("Author", "Moreno, Jordao")
),
new XElement("Book",
new XElement("Title", "Midieval Tools and Implements"),
new XElement("Author", "Gazit, Inbar")
)
),
new XComment("This is another comment."));
Can someone please help me on this?
here is sample XML file which I want to generate using XDocument
There is a much simpler way to do this rather than crafting the XML document by hand via XDocument, though I have an explanation and example below if you want to do it that way.
First, the simple way - create the XML as a string, and pass that string to XDocument.Parse, like this:
string xmlString = #"<S:Envelope xmlns:S=""http://schemas.xmlsoap.org/soap/envelope/""><S:Header xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""><ns13:ACASecurityHeader xmlns:ns10=""urn:us:gov:treasury:irs:msg:acauibusinessheader"" xmlns:ns11=""http://www.w3.org/2000/09/xmldsig#"" xmlns:ns12=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:ns13=""urn:us:gov:treasury:irs:msg:acasecurityheader"" xmlns:ns2=""urn:us:gov:treasury:irs:common"" xmlns:ns3=""urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage"" xmlns:ns4=""urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"" xmlns:ns5=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"" xmlns:ns6=""urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage"" xmlns:ns7=""urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage"" xmlns:ns8=""urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter"" xmlns:ns9=""urn:us:gov:treasury:irs:msg:acabusinessheader""><Author>Moreno, Jordao</Author><Book><Title>Midieval Tools and Implement</Title><Author>Gazit, Inbar</Author></Book></ns13:ACASecurityHeader><!--This is another comment--></S:Header></S:Envelope>";
XDocument xDoc2 = XDocument.Parse(xmlString);
xDoc2 will contain the XML you wish to send.
If you wish to do it the long way, then there are a couple of issues with your posted code.
First, you're not correctly handling the namespaces (the xmlns: attributes). Secondly, you're including the < and > in the call to XElement, and you don't need to do that - the method takes care of those two symbols.
What you need to do is to set up the namespaces, then add them to the appropriate elements as well as creating the attributes for them.
The sample code doesn't match the posted snippet, so I worked off your sample code to show you how to go about crafting the XML by hand.
XNamespace sNS = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/");
XNamespace wsseNS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XNamespace xmlnsNS = XNamespace.Get("urn:us:gov:treasury:irs:ext:aca:air:7.0");
XNamespace ns10NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acauibusinessheader");
XNamespace ns11NS = XNamespace.Get("http://www.w3.org/2000/09/xmldsig#");
XNamespace ns12NS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XNamespace ns13NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acasecurityheader");
XNamespace ns2NS = XNamespace.Get("xmlns: ns2 = 'urn:us:gov:treasury:irs:common");
XNamespace ns3NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage");
XNamespace ns4NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage");
XNamespace ns5NS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
XNamespace ns6NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage");
XNamespace ns7NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage");
XNamespace ns8NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter");
XNamespace ns9NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acabusinessheader");
XDocument xDoc = new XDocument(new XElement(sNS + "Envelope", new XAttribute(XNamespace.Xmlns + "S", sNS),
new XElement(sNS + "Header", new XAttribute(XNamespace.Xmlns + "wsse", wsseNS),
new XElement(ns13NS + "ACASecurityHeader", new XAttribute(XNamespace.Xmlns + "ns10", ns10NS),
new XAttribute(XNamespace.Xmlns + "ns11", ns11NS),
new XAttribute(XNamespace.Xmlns + "ns12", ns12NS),
new XAttribute(XNamespace.Xmlns + "ns13", ns13NS),
new XAttribute(XNamespace.Xmlns + "ns2", ns2NS),
new XAttribute(XNamespace.Xmlns + "ns3", ns3NS),
new XAttribute(XNamespace.Xmlns + "ns4", ns4NS),
new XAttribute(XNamespace.Xmlns + "ns5", ns5NS),
new XAttribute(XNamespace.Xmlns + "ns6", ns6NS),
new XAttribute(XNamespace.Xmlns + "ns7", ns7NS),
new XAttribute(XNamespace.Xmlns + "ns8", ns8NS),
new XAttribute(XNamespace.Xmlns + "ns9", ns9NS
new XAttribute("xmlns", xmlnsNS),
new XElement("Author", "Moreno, Jordao"),
new XElement("Book",
new XElement("Title", "Midieval Tools and Implement"),
new XElement("Author", "Gazit, Inbar"))
),
new XComment("This is another comment")
))
);
The first thing the above code does is sets up all the namespaces via XNamespace.
Next, the XML Document is constructed. The individual elements are created via XElement, with the various namespaces prefixed (i.e., new XElement(sNS + "Envelope",, and the other namespaces added via XAttribute.
Nesting can get tricky, so you have to be very careful doing it this way. The above code will produce the following XML:
<?xml version="1.0"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<ns13:ACASecurityHeader xmlns="urn:us:gov:treasury:irs:ext:aca:air:7.0"
xmlns:ns9="urn:us:gov:treasury:irs:msg:acabusinessheader"
xmlns:ns8="urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter"
xmlns:ns7="urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage"
xmlns:ns6="urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage"
xmlns:ns5="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:ns4="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"
xmlns:ns3="urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage" xmlns:ns2="urn:us:gov:treasury:irs:common"
xmlns:ns13="urn:us:gov:treasury:irs:msg:acasecurityheader"
xmlns:ns12="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:ns11="http://www.w3.org/2000/09/xmldsig#"
xmlns:ns10="urn:us:gov:treasury:irs:msg:acauibusinessheader">
<Author>Moreno, Jordao</Author>
<Book>
<Title>Midieval Tools and Implement</Title>
<Author>Gazit, Inbar</Author>
</Book>
</ns13:ACASecurityHeader>
<!--This is another comment-->
</S:Header>
</S:Envelope>
What you're doing is a really hard way to do this. There is a much easier way.
You have the Xsd specifications from them, you can use the xsd command in the Visual Studio command line to generate C# objects that match the requirements automatically during serialization.
For the IRS ACA schemas, get all the XSD files into the same directory. Then in a sibling directory to the one you created, place the Common folder.
Then, in the command line navigate to the directory you created and put all the xsd files and run this command:
xsd /c IRS-EXT-ACA-AIR-7.0.xsd IRS-ACABulkRequestTransmitterMessage.xsd IRS-Form1094-1095CTransmitterUpstreamMessage.xsd IRS-CAC.xsd IRS-WSTimeStampElementMessage.xsd IRS-WSTimeStampElementMessage.xsd
You'll end up with a C# file that has almost 200 objects in it including all the enums and such necessary for generating data compliant with their specifications.

I can't get <pmlcore:Sensor with XElement

I'm developing an ASP.NET MVC application with .NET Framework 4.5.1 that returns XML generated from database data.
I want to obtain this:
<?xml version="1.0" encoding="utf-8"?>
<pmlcore:Sensor [ Ommitted for brevety ] ">
But I get this:
<?xml version="1.0" encoding="utf-8"?>
<Sensor [ Ommitted for brevety ] xmlns="pmlcore">
Reading all the answers found in Stackoverflow, I changed my code to use XNamespace:
XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
XDeclaration dec = new XDeclaration("1.0", "utf-8", null);
XNamespace pmlcore = "pmlcore";
XNamespace pmluid = "pmluid";
root = new XElement(pmlcore + "Sensor"
, new XAttribute(XNamespace.Xmlns + "pmluid",
"urn:autoid:specification:universal:Identifier:xml:schema:1")
, new XAttribute(XNamespace.Xmlns + "xsi", ns)
, new XAttribute(XNamespace.Xmlns + "pmlcore",
"urn:autoid:specification:interchange:PMLCore:xml:schema:1")
, new XAttribute(ns + "noNamespaceSchemaLocation",
"urn:autoid:specification:interchange:PMLCore:xml:schema:1 ./PML/SchemaFiles/Interchange/PMLCore.xsd")
How can I obtain <pmlcore:Sensor this?
If I use this code:
root = new XElement("pmlcore:Sensor"
I get this error:
The ':' character, hexadecimal value 0x3A, cannot be included in a
name
The problem is that you're adding the wrong namespace... you're trying to use the alias for it, instead of the namespace URI. Here's a concrete example that works:
using System;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
XNamespace pmlcore = "urn:autoid:specification:interchange:PMLCore:xml:schema:1";
XNamespace pmluid = "urn:autoid:specification:universal:Identifier:xml:schema:1";
var root = new XElement(pmlcore + "Sensor",
new XAttribute(XNamespace.Xmlns + "pmluid", pmluid.NamespaceName),
new XAttribute(XNamespace.Xmlns + "pmlcore", pmlcore.NamespaceName));
Console.WriteLine(root);
}
}
Output (reformatted):
<pmlcore:Sensor
xmlns:pmluid="urn:autoid:specification:universal:Identifier:xml:schema:1"
xmlns:pmlcore="urn:autoid:specification:interchange:PMLCore:xml:schema:1" />

how to handle namespaces in the root element

I am reading from a csv file and transferring the data to an xml file using XmlSerializer in c#.But now I am facing a problem with the namespaces in the root element.My required xml should be in the following format.
<?xml version="1.0" encoding="ASCII"?>
<abc:Country xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:abc="some url">
<Person></Person>
</abc:Country>
But I am getting my output in this format:
<?xml version="1.0" encoding="ASCII"?>
<Country xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Person></Person>
</Country>
I want the namespace of abc to be included in the root and then also "abc" should prefix only my root element i.e "Country". I tried using various options mentioned online but none of them actually worked for me.When i use the XmlSerializerNamespaces and overload my Serialiser class all opther namespaces disappear.So could you let me know how I could achieve this.
Is it important to use XmlSerializer? This sort of thing is pretty easy to do with XDocument instead. Something like this:
var document = new XDocument();
XNamespace abcns = "http://some/url/abc";
XNamespace xmins = "http://www.omg.org/XMI";
XNamespace xsins = "http://www.w3.org/2001/XMLSchema-instance";
var element = new XElement(abcns + "Country",
new XAttribute(XNamespace.Xmlns + "abc", abcns),
new XAttribute(XNamespace.Xmlns + "xmi", xmins),
new XAttribute(XNamespace.Xmlns + "xsi", xsins),
new XAttribute(xmins + "version", "2.0"),
new XElement("Person"));
document.Add(element);
We can use the following to include multiple namespaces in the root element of the xml:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xmi", "http://www.omg.org/XMI");
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.Add("abc", "some url");
XmlSerializer serializer = new XmlSerializer(typeof(Country));
TextWriter textWriter = new StreamWriter(#"C:\test.xml", true, Encoding.ASCII);
serializer.Serialize(textWriter, country, ns);
"country" would be the object that you would be creating for the class "Country"(root element of your xml).

XNamespace url is written in XAttribute when I create my XML

I'm creating a XML with namespaces but url are written in XAttribute elements instead of Namespace "prefix"
This is what I get as output
<xmi:XMI xmlns:uml="http://www.omg.org/spec/UML/20110701"
xmlns:xmi="http://www.omg.org/spec/XMI/20110701">
<xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5" />
<uml:Model xmi:type="{http://www.omg.org/spec/UML/20110701}Model" name="EA_Model">
<packagedElement xmi:type="{http://www.omg.org/spec/UML/20110701}Package" xmi:id="1212" name="Logical View">
<packagedElement xmi:type="{http://www.omg.org/spec/UML/20110701}Class" xmi:id="35798" name="MyClass">
but I should generate something like this..
<xmi:XMI xmlns:uml="http://www.omg.org/spec/UML/20110701"
xmlns:xmi="http://www.omg.org/spec/XMI/20110701">
<xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5" />
<uml:Model xmi:type="uml:Model" name="EA_Model">
<packagedElement xmi:type="uml:Package" xmi:id="1212" name="Logical View">
<packagedElement xmi:type="uml:Class" xmi:id="35798" name="MyClass">
My Code:
XNamespace uml = "http://www.omg.org/spec/UML/20110701";
XNamespace xmi = "http://www.omg.org/spec/XMI/20110701";
XElement rootElement = new XElement(xmi + "XMI", new XAttribute(XNamespace.Xmlns + "uml", "http://www.omg.org/spec/UML/20110701"), new XAttribute(XNamespace.Xmlns + "xmi", "http://www.omg.org/spec/XMI/20110701"));
doc.Add(rootElement);
rootElement.Add(new XElement(xmi+"Documentation", new XAttribute("exporter", "Enterprise Architect"), new XAttribute("exporterVersion", "6.5")));
XElement modelNode = new XElement(uml + "Model", new XAttribute(xmi + "type", uml + "Model"), new XAttribute("name", "EA_Model"));
rootElement.Add(modelNode);
XElement logicalViewElement = new XElement("packagedElement", new XAttribute(xmi + "type", uml + "Package"), new XAttribute(xmi + "id", project.Id), new XAttribute("name", "Logical View"));
modelNode.Add(logicalViewElement);
Dictionary<string, XElement> entityNodes = new Dictionary<string, XElement>();
foreach (BusinessEntityDataObject entity in project.BusinessEntities)
{
XElement entityElement =
new XElement("packagedElement",
new XAttribute(xmi+"type",uml+"Class"),
new XAttribute(xmi+"id",entity.Id),
new XAttribute("name",entity.Name));
Instead of concatenating XNamespace with string value
new XAttribute(xmi + "type", uml + "Class")
You should build attribute value manually:
new XAttribute(xmi + "type", "uml:Class")
XAttribute constructor has two parameters - first one expects XName and second one expects simple object. When you combine XNamespace with string you get string result, which looks like xmi.ToString() + attributeName, which equals "{http://www.omg.org/spec/XMI/20110701}type". Thus XNode type has defined implicit conversion from string, this string can be converted to XName instance.
In second case uml + "Class" also combined into string "{http://www.omg.org/spec/UML/20110701}Class". But second argument of constructor should be of type object, and this string satisfies that requirement, so nothing is converted to XName and you have your combined string as attribute value.

Categories