Converting XmlElement to XElement

Today at work I had to use 2 APIs which fellow team members had written. One had used standard .NET xml classes, whilst the other had used XLINQ, and I had to get an XmlElement from one API and pass a XElement to the other API.

This was pretty weird syntax so I thought I would share it here in case anyone else stumbles on this

Here it is in it’s entirety

public XElement GetXElementFromXmlElement(XmlElement xmlElement)
{
      return XElement.Load(xmlElement.CreateNavigator().ReadSubtree());
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Hope that helps you
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

About these ads

3 thoughts on “Converting XmlElement to XElement

  1. ThaStyle says:

    Three Freakin lines of code, u just made my day again, why is it that i always end up at ur site or @ codeplex reading ur stuff, it seems like evereyting i do, you either have done or still do.. anyways just wanted 2 say thanks. i always said knowledge is sharing, seems to be ur life guide to, i have some questions can i send u an email ?
    basicly i need to extend an old peace of work dotnet2 with new stuff and i can not just replace that peace of shit since a company is using it (100people) so i thought about injecting it with an module loader that acts as an dotnet4 adapter, i think i can do this, but if i can do this how do i protect it, even my little cousin of 13 knows snoop or reflector, if i may i will send u some of the things i have ?? about
    Thanks already
    Greets
    Style (peter)

Comments are closed.