How to convert XmlNode into XElement?












20














I have an old XmlNode-based code. but the simplest way to solve my current task is to use XElement and LINQ-to-XML. The only problem is that there is no direct or obvious method for converting a XmlNode to a XElement in .NET Framework.



So for starters, I want to implement a method that receives a XmlNode instance and converts it to a XElement instance.



How can I implement this conversion?










share|improve this question
























  • Are all of your XmlNode instances XmlElement instances?
    – John Saunders
    Jul 14 '14 at 19:19






  • 1




    @JohnSaunders Yes, all the XmlNode instances are XmlElement instances.
    – Alexander Abakumov
    Jul 14 '14 at 19:36






  • 1




    @JohnSaunders, Sorry, not all the XmlNode instances are XmlElement instances. They might be, for example, XmlComment instances.
    – Alexander Abakumov
    Jul 15 '14 at 9:04
















20














I have an old XmlNode-based code. but the simplest way to solve my current task is to use XElement and LINQ-to-XML. The only problem is that there is no direct or obvious method for converting a XmlNode to a XElement in .NET Framework.



So for starters, I want to implement a method that receives a XmlNode instance and converts it to a XElement instance.



How can I implement this conversion?










share|improve this question
























  • Are all of your XmlNode instances XmlElement instances?
    – John Saunders
    Jul 14 '14 at 19:19






  • 1




    @JohnSaunders Yes, all the XmlNode instances are XmlElement instances.
    – Alexander Abakumov
    Jul 14 '14 at 19:36






  • 1




    @JohnSaunders, Sorry, not all the XmlNode instances are XmlElement instances. They might be, for example, XmlComment instances.
    – Alexander Abakumov
    Jul 15 '14 at 9:04














20












20








20


1





I have an old XmlNode-based code. but the simplest way to solve my current task is to use XElement and LINQ-to-XML. The only problem is that there is no direct or obvious method for converting a XmlNode to a XElement in .NET Framework.



So for starters, I want to implement a method that receives a XmlNode instance and converts it to a XElement instance.



How can I implement this conversion?










share|improve this question















I have an old XmlNode-based code. but the simplest way to solve my current task is to use XElement and LINQ-to-XML. The only problem is that there is no direct or obvious method for converting a XmlNode to a XElement in .NET Framework.



So for starters, I want to implement a method that receives a XmlNode instance and converts it to a XElement instance.



How can I implement this conversion?







c# .net linq linq-to-xml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 '16 at 23:19

























asked Jul 14 '14 at 19:05









Alexander Abakumov

4,38744067




4,38744067












  • Are all of your XmlNode instances XmlElement instances?
    – John Saunders
    Jul 14 '14 at 19:19






  • 1




    @JohnSaunders Yes, all the XmlNode instances are XmlElement instances.
    – Alexander Abakumov
    Jul 14 '14 at 19:36






  • 1




    @JohnSaunders, Sorry, not all the XmlNode instances are XmlElement instances. They might be, for example, XmlComment instances.
    – Alexander Abakumov
    Jul 15 '14 at 9:04


















  • Are all of your XmlNode instances XmlElement instances?
    – John Saunders
    Jul 14 '14 at 19:19






  • 1




    @JohnSaunders Yes, all the XmlNode instances are XmlElement instances.
    – Alexander Abakumov
    Jul 14 '14 at 19:36






  • 1




    @JohnSaunders, Sorry, not all the XmlNode instances are XmlElement instances. They might be, for example, XmlComment instances.
    – Alexander Abakumov
    Jul 15 '14 at 9:04
















Are all of your XmlNode instances XmlElement instances?
– John Saunders
Jul 14 '14 at 19:19




Are all of your XmlNode instances XmlElement instances?
– John Saunders
Jul 14 '14 at 19:19




1




1




@JohnSaunders Yes, all the XmlNode instances are XmlElement instances.
– Alexander Abakumov
Jul 14 '14 at 19:36




@JohnSaunders Yes, all the XmlNode instances are XmlElement instances.
– Alexander Abakumov
Jul 14 '14 at 19:36




1




1




@JohnSaunders, Sorry, not all the XmlNode instances are XmlElement instances. They might be, for example, XmlComment instances.
– Alexander Abakumov
Jul 15 '14 at 9:04




@JohnSaunders, Sorry, not all the XmlNode instances are XmlElement instances. They might be, for example, XmlComment instances.
– Alexander Abakumov
Jul 15 '14 at 9:04












5 Answers
5






active

oldest

votes


















17














var xElem = XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );


There are two problems with xmlElement.InnerXml used in other answer,



1- You will loose the root element (Of course, it can be handled easily)



XmlDocument doc = new XmlDocument();
doc.LoadXml("<root> <sub>aaa</sub> </root>");
var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml);


xElem2 will be <sub>aaa</sub>, without(root)



2- You will get exception if your xml contains text nodes



XmlDocument doc = new XmlDocument();
doc.LoadXml("<root> text <sub>aaa</sub> </root>");
var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml); //<-- XmlException





share|improve this answer



















  • 1




    Thanks for the great solution using XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );! This is perfect if You need to convert to XElement instances of the XmlElement only. But for an XmlComment instance ReadSubtree() throws: System.InvalidOperationException: Operation is not valid due to the current position of the navigator.
    – Alexander Abakumov
    Jul 15 '14 at 9:14








  • 2




    For others who would use this approach, make sure that the NodeType of all your XmlNodes is the Element.
    – Alexander Abakumov
    Jul 15 '14 at 9:30



















4














You can try using InnerXml property of XmlElement to get xml content of your element then parse it to XElement using XElement.Parse:



public static XElement ToXELement(this XmlElement source)
{
return XElement.Parse(source.InnerXml);
}





share|improve this answer

















  • 3




    Why not use OuterXml instead of InnerXml?
    – Jenny O'Reilly
    Aug 7 '15 at 7:09



















-1














just use it: XElement e = XElement.Load(node.CreateReader());



I hope that helps.



Example:



A real code example
The image code above




public static XNode GetNodeByFilter(XNode node,ref SortedList filter, int position = 1)
{



XNode result = null;



        if (filter.TryGetValue(position, out XMLSearchCriteria criteria))
{
while (node != null)
{
XElement e = XElement.Load(node.CreateReader());
if (e.Name.LocalName.Equals(criteria.Node) && CheckIfAllAttributesMatch(e.Attributes(), criteria.Attributes))
{
if (++position <= filter.Count)
{
result = GetNodeByFilter(e.FirstNode, ref filter, position);
break;
}
else
{
result = node;
}
}

node = node.NextNode;
}
}

return result;
}






share|improve this answer































    -2














    There actually is a very straightforward way to convert an XNode to an XElement:



    static XElement ToXElement( XNode node)
    {
    return node as XElement; // returns null if node is not an XElement
    }


    If you are 100% certain the node is an XElement (or you are prepared to deal with the exception if it is not, then you can simply cast: (XElement)node.






    share|improve this answer

















    • 1




      The question is about converting XmlNode, not XNode.
      – Alexander Abakumov
      Nov 17 '17 at 21:18



















    -8














    As far as I know, you can do this:



    XElement xdoc = new XElement(node.Name, node.InnerXml);





    share|improve this answer





















    • Absolutely no, this will escape to whole xml and you will get a single node like <root> &lt;sub&gt;aaa&lt;/sub&gt; </root>
      – EZI
      Jul 14 '14 at 23:42










    • Sorry, but, how do you know his xml is like "<root> <sub>aaa</sub> </root>"? Maybe his nodes are simplier than that.
      – MJVC
      Jul 15 '14 at 0:01










    • Marcos, simpler or not, your code wouldn't work. It escapes and converts all innerXml to string. What don't you simply copy and paste my 3-lines code and test it with your solution. No need to discuss about which can be tested easily.
      – EZI
      Jul 15 '14 at 0:06












    • Your 3 lines? Again, maybe his XML is simplier.
      – MJVC
      Jul 15 '14 at 0:08






    • 5




      Marcos, Come on, Please....., It would take your a few minutes. Take my codes, append your solution, open VS and test them with any xml you want. NO YOUR SOLUTION DOESN'T WORK
      – EZI
      Jul 15 '14 at 0:23













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24743916%2fhow-to-convert-xmlnode-into-xelement%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    17














    var xElem = XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );


    There are two problems with xmlElement.InnerXml used in other answer,



    1- You will loose the root element (Of course, it can be handled easily)



    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root> <sub>aaa</sub> </root>");
    var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
    var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml);


    xElem2 will be <sub>aaa</sub>, without(root)



    2- You will get exception if your xml contains text nodes



    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root> text <sub>aaa</sub> </root>");
    var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
    var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml); //<-- XmlException





    share|improve this answer



















    • 1




      Thanks for the great solution using XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );! This is perfect if You need to convert to XElement instances of the XmlElement only. But for an XmlComment instance ReadSubtree() throws: System.InvalidOperationException: Operation is not valid due to the current position of the navigator.
      – Alexander Abakumov
      Jul 15 '14 at 9:14








    • 2




      For others who would use this approach, make sure that the NodeType of all your XmlNodes is the Element.
      – Alexander Abakumov
      Jul 15 '14 at 9:30
















    17














    var xElem = XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );


    There are two problems with xmlElement.InnerXml used in other answer,



    1- You will loose the root element (Of course, it can be handled easily)



    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root> <sub>aaa</sub> </root>");
    var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
    var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml);


    xElem2 will be <sub>aaa</sub>, without(root)



    2- You will get exception if your xml contains text nodes



    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root> text <sub>aaa</sub> </root>");
    var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
    var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml); //<-- XmlException





    share|improve this answer



















    • 1




      Thanks for the great solution using XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );! This is perfect if You need to convert to XElement instances of the XmlElement only. But for an XmlComment instance ReadSubtree() throws: System.InvalidOperationException: Operation is not valid due to the current position of the navigator.
      – Alexander Abakumov
      Jul 15 '14 at 9:14








    • 2




      For others who would use this approach, make sure that the NodeType of all your XmlNodes is the Element.
      – Alexander Abakumov
      Jul 15 '14 at 9:30














    17












    17








    17






    var xElem = XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );


    There are two problems with xmlElement.InnerXml used in other answer,



    1- You will loose the root element (Of course, it can be handled easily)



    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root> <sub>aaa</sub> </root>");
    var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
    var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml);


    xElem2 will be <sub>aaa</sub>, without(root)



    2- You will get exception if your xml contains text nodes



    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root> text <sub>aaa</sub> </root>");
    var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
    var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml); //<-- XmlException





    share|improve this answer














    var xElem = XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );


    There are two problems with xmlElement.InnerXml used in other answer,



    1- You will loose the root element (Of course, it can be handled easily)



    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root> <sub>aaa</sub> </root>");
    var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
    var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml);


    xElem2 will be <sub>aaa</sub>, without(root)



    2- You will get exception if your xml contains text nodes



    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<root> text <sub>aaa</sub> </root>");
    var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
    var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml); //<-- XmlException






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 14 '14 at 21:38

























    answered Jul 14 '14 at 20:48









    EZI

    13.1k11728




    13.1k11728








    • 1




      Thanks for the great solution using XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );! This is perfect if You need to convert to XElement instances of the XmlElement only. But for an XmlComment instance ReadSubtree() throws: System.InvalidOperationException: Operation is not valid due to the current position of the navigator.
      – Alexander Abakumov
      Jul 15 '14 at 9:14








    • 2




      For others who would use this approach, make sure that the NodeType of all your XmlNodes is the Element.
      – Alexander Abakumov
      Jul 15 '14 at 9:30














    • 1




      Thanks for the great solution using XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );! This is perfect if You need to convert to XElement instances of the XmlElement only. But for an XmlComment instance ReadSubtree() throws: System.InvalidOperationException: Operation is not valid due to the current position of the navigator.
      – Alexander Abakumov
      Jul 15 '14 at 9:14








    • 2




      For others who would use this approach, make sure that the NodeType of all your XmlNodes is the Element.
      – Alexander Abakumov
      Jul 15 '14 at 9:30








    1




    1




    Thanks for the great solution using XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );! This is perfect if You need to convert to XElement instances of the XmlElement only. But for an XmlComment instance ReadSubtree() throws: System.InvalidOperationException: Operation is not valid due to the current position of the navigator.
    – Alexander Abakumov
    Jul 15 '14 at 9:14






    Thanks for the great solution using XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );! This is perfect if You need to convert to XElement instances of the XmlElement only. But for an XmlComment instance ReadSubtree() throws: System.InvalidOperationException: Operation is not valid due to the current position of the navigator.
    – Alexander Abakumov
    Jul 15 '14 at 9:14






    2




    2




    For others who would use this approach, make sure that the NodeType of all your XmlNodes is the Element.
    – Alexander Abakumov
    Jul 15 '14 at 9:30




    For others who would use this approach, make sure that the NodeType of all your XmlNodes is the Element.
    – Alexander Abakumov
    Jul 15 '14 at 9:30













    4














    You can try using InnerXml property of XmlElement to get xml content of your element then parse it to XElement using XElement.Parse:



    public static XElement ToXELement(this XmlElement source)
    {
    return XElement.Parse(source.InnerXml);
    }





    share|improve this answer

















    • 3




      Why not use OuterXml instead of InnerXml?
      – Jenny O'Reilly
      Aug 7 '15 at 7:09
















    4














    You can try using InnerXml property of XmlElement to get xml content of your element then parse it to XElement using XElement.Parse:



    public static XElement ToXELement(this XmlElement source)
    {
    return XElement.Parse(source.InnerXml);
    }





    share|improve this answer

















    • 3




      Why not use OuterXml instead of InnerXml?
      – Jenny O'Reilly
      Aug 7 '15 at 7:09














    4












    4








    4






    You can try using InnerXml property of XmlElement to get xml content of your element then parse it to XElement using XElement.Parse:



    public static XElement ToXELement(this XmlElement source)
    {
    return XElement.Parse(source.InnerXml);
    }





    share|improve this answer












    You can try using InnerXml property of XmlElement to get xml content of your element then parse it to XElement using XElement.Parse:



    public static XElement ToXELement(this XmlElement source)
    {
    return XElement.Parse(source.InnerXml);
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 14 '14 at 19:10









    Selman Genç

    83.5k1077139




    83.5k1077139








    • 3




      Why not use OuterXml instead of InnerXml?
      – Jenny O'Reilly
      Aug 7 '15 at 7:09














    • 3




      Why not use OuterXml instead of InnerXml?
      – Jenny O'Reilly
      Aug 7 '15 at 7:09








    3




    3




    Why not use OuterXml instead of InnerXml?
    – Jenny O'Reilly
    Aug 7 '15 at 7:09




    Why not use OuterXml instead of InnerXml?
    – Jenny O'Reilly
    Aug 7 '15 at 7:09











    -1














    just use it: XElement e = XElement.Load(node.CreateReader());



    I hope that helps.



    Example:



    A real code example
    The image code above




    public static XNode GetNodeByFilter(XNode node,ref SortedList filter, int position = 1)
    {



    XNode result = null;



            if (filter.TryGetValue(position, out XMLSearchCriteria criteria))
    {
    while (node != null)
    {
    XElement e = XElement.Load(node.CreateReader());
    if (e.Name.LocalName.Equals(criteria.Node) && CheckIfAllAttributesMatch(e.Attributes(), criteria.Attributes))
    {
    if (++position <= filter.Count)
    {
    result = GetNodeByFilter(e.FirstNode, ref filter, position);
    break;
    }
    else
    {
    result = node;
    }
    }

    node = node.NextNode;
    }
    }

    return result;
    }






    share|improve this answer




























      -1














      just use it: XElement e = XElement.Load(node.CreateReader());



      I hope that helps.



      Example:



      A real code example
      The image code above




      public static XNode GetNodeByFilter(XNode node,ref SortedList filter, int position = 1)
      {



      XNode result = null;



              if (filter.TryGetValue(position, out XMLSearchCriteria criteria))
      {
      while (node != null)
      {
      XElement e = XElement.Load(node.CreateReader());
      if (e.Name.LocalName.Equals(criteria.Node) && CheckIfAllAttributesMatch(e.Attributes(), criteria.Attributes))
      {
      if (++position <= filter.Count)
      {
      result = GetNodeByFilter(e.FirstNode, ref filter, position);
      break;
      }
      else
      {
      result = node;
      }
      }

      node = node.NextNode;
      }
      }

      return result;
      }






      share|improve this answer


























        -1












        -1








        -1






        just use it: XElement e = XElement.Load(node.CreateReader());



        I hope that helps.



        Example:



        A real code example
        The image code above




        public static XNode GetNodeByFilter(XNode node,ref SortedList filter, int position = 1)
        {



        XNode result = null;



                if (filter.TryGetValue(position, out XMLSearchCriteria criteria))
        {
        while (node != null)
        {
        XElement e = XElement.Load(node.CreateReader());
        if (e.Name.LocalName.Equals(criteria.Node) && CheckIfAllAttributesMatch(e.Attributes(), criteria.Attributes))
        {
        if (++position <= filter.Count)
        {
        result = GetNodeByFilter(e.FirstNode, ref filter, position);
        break;
        }
        else
        {
        result = node;
        }
        }

        node = node.NextNode;
        }
        }

        return result;
        }






        share|improve this answer














        just use it: XElement e = XElement.Load(node.CreateReader());



        I hope that helps.



        Example:



        A real code example
        The image code above




        public static XNode GetNodeByFilter(XNode node,ref SortedList filter, int position = 1)
        {



        XNode result = null;



                if (filter.TryGetValue(position, out XMLSearchCriteria criteria))
        {
        while (node != null)
        {
        XElement e = XElement.Load(node.CreateReader());
        if (e.Name.LocalName.Equals(criteria.Node) && CheckIfAllAttributesMatch(e.Attributes(), criteria.Attributes))
        {
        if (++position <= filter.Count)
        {
        result = GetNodeByFilter(e.FirstNode, ref filter, position);
        break;
        }
        else
        {
        result = node;
        }
        }

        node = node.NextNode;
        }
        }

        return result;
        }







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 14:51

























        answered Nov 21 '18 at 14:36









        Gustavo Lemos

        11




        11























            -2














            There actually is a very straightforward way to convert an XNode to an XElement:



            static XElement ToXElement( XNode node)
            {
            return node as XElement; // returns null if node is not an XElement
            }


            If you are 100% certain the node is an XElement (or you are prepared to deal with the exception if it is not, then you can simply cast: (XElement)node.






            share|improve this answer

















            • 1




              The question is about converting XmlNode, not XNode.
              – Alexander Abakumov
              Nov 17 '17 at 21:18
















            -2














            There actually is a very straightforward way to convert an XNode to an XElement:



            static XElement ToXElement( XNode node)
            {
            return node as XElement; // returns null if node is not an XElement
            }


            If you are 100% certain the node is an XElement (or you are prepared to deal with the exception if it is not, then you can simply cast: (XElement)node.






            share|improve this answer

















            • 1




              The question is about converting XmlNode, not XNode.
              – Alexander Abakumov
              Nov 17 '17 at 21:18














            -2












            -2








            -2






            There actually is a very straightforward way to convert an XNode to an XElement:



            static XElement ToXElement( XNode node)
            {
            return node as XElement; // returns null if node is not an XElement
            }


            If you are 100% certain the node is an XElement (or you are prepared to deal with the exception if it is not, then you can simply cast: (XElement)node.






            share|improve this answer












            There actually is a very straightforward way to convert an XNode to an XElement:



            static XElement ToXElement( XNode node)
            {
            return node as XElement; // returns null if node is not an XElement
            }


            If you are 100% certain the node is an XElement (or you are prepared to deal with the exception if it is not, then you can simply cast: (XElement)node.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 17 '17 at 19:12









            Mark Moore

            225




            225








            • 1




              The question is about converting XmlNode, not XNode.
              – Alexander Abakumov
              Nov 17 '17 at 21:18














            • 1




              The question is about converting XmlNode, not XNode.
              – Alexander Abakumov
              Nov 17 '17 at 21:18








            1




            1




            The question is about converting XmlNode, not XNode.
            – Alexander Abakumov
            Nov 17 '17 at 21:18




            The question is about converting XmlNode, not XNode.
            – Alexander Abakumov
            Nov 17 '17 at 21:18











            -8














            As far as I know, you can do this:



            XElement xdoc = new XElement(node.Name, node.InnerXml);





            share|improve this answer





















            • Absolutely no, this will escape to whole xml and you will get a single node like <root> &lt;sub&gt;aaa&lt;/sub&gt; </root>
              – EZI
              Jul 14 '14 at 23:42










            • Sorry, but, how do you know his xml is like "<root> <sub>aaa</sub> </root>"? Maybe his nodes are simplier than that.
              – MJVC
              Jul 15 '14 at 0:01










            • Marcos, simpler or not, your code wouldn't work. It escapes and converts all innerXml to string. What don't you simply copy and paste my 3-lines code and test it with your solution. No need to discuss about which can be tested easily.
              – EZI
              Jul 15 '14 at 0:06












            • Your 3 lines? Again, maybe his XML is simplier.
              – MJVC
              Jul 15 '14 at 0:08






            • 5




              Marcos, Come on, Please....., It would take your a few minutes. Take my codes, append your solution, open VS and test them with any xml you want. NO YOUR SOLUTION DOESN'T WORK
              – EZI
              Jul 15 '14 at 0:23


















            -8














            As far as I know, you can do this:



            XElement xdoc = new XElement(node.Name, node.InnerXml);





            share|improve this answer





















            • Absolutely no, this will escape to whole xml and you will get a single node like <root> &lt;sub&gt;aaa&lt;/sub&gt; </root>
              – EZI
              Jul 14 '14 at 23:42










            • Sorry, but, how do you know his xml is like "<root> <sub>aaa</sub> </root>"? Maybe his nodes are simplier than that.
              – MJVC
              Jul 15 '14 at 0:01










            • Marcos, simpler or not, your code wouldn't work. It escapes and converts all innerXml to string. What don't you simply copy and paste my 3-lines code and test it with your solution. No need to discuss about which can be tested easily.
              – EZI
              Jul 15 '14 at 0:06












            • Your 3 lines? Again, maybe his XML is simplier.
              – MJVC
              Jul 15 '14 at 0:08






            • 5




              Marcos, Come on, Please....., It would take your a few minutes. Take my codes, append your solution, open VS and test them with any xml you want. NO YOUR SOLUTION DOESN'T WORK
              – EZI
              Jul 15 '14 at 0:23
















            -8












            -8








            -8






            As far as I know, you can do this:



            XElement xdoc = new XElement(node.Name, node.InnerXml);





            share|improve this answer












            As far as I know, you can do this:



            XElement xdoc = new XElement(node.Name, node.InnerXml);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 14 '14 at 23:32









            MJVC

            48147




            48147












            • Absolutely no, this will escape to whole xml and you will get a single node like <root> &lt;sub&gt;aaa&lt;/sub&gt; </root>
              – EZI
              Jul 14 '14 at 23:42










            • Sorry, but, how do you know his xml is like "<root> <sub>aaa</sub> </root>"? Maybe his nodes are simplier than that.
              – MJVC
              Jul 15 '14 at 0:01










            • Marcos, simpler or not, your code wouldn't work. It escapes and converts all innerXml to string. What don't you simply copy and paste my 3-lines code and test it with your solution. No need to discuss about which can be tested easily.
              – EZI
              Jul 15 '14 at 0:06












            • Your 3 lines? Again, maybe his XML is simplier.
              – MJVC
              Jul 15 '14 at 0:08






            • 5




              Marcos, Come on, Please....., It would take your a few minutes. Take my codes, append your solution, open VS and test them with any xml you want. NO YOUR SOLUTION DOESN'T WORK
              – EZI
              Jul 15 '14 at 0:23




















            • Absolutely no, this will escape to whole xml and you will get a single node like <root> &lt;sub&gt;aaa&lt;/sub&gt; </root>
              – EZI
              Jul 14 '14 at 23:42










            • Sorry, but, how do you know his xml is like "<root> <sub>aaa</sub> </root>"? Maybe his nodes are simplier than that.
              – MJVC
              Jul 15 '14 at 0:01










            • Marcos, simpler or not, your code wouldn't work. It escapes and converts all innerXml to string. What don't you simply copy and paste my 3-lines code and test it with your solution. No need to discuss about which can be tested easily.
              – EZI
              Jul 15 '14 at 0:06












            • Your 3 lines? Again, maybe his XML is simplier.
              – MJVC
              Jul 15 '14 at 0:08






            • 5




              Marcos, Come on, Please....., It would take your a few minutes. Take my codes, append your solution, open VS and test them with any xml you want. NO YOUR SOLUTION DOESN'T WORK
              – EZI
              Jul 15 '14 at 0:23


















            Absolutely no, this will escape to whole xml and you will get a single node like <root> &lt;sub&gt;aaa&lt;/sub&gt; </root>
            – EZI
            Jul 14 '14 at 23:42




            Absolutely no, this will escape to whole xml and you will get a single node like <root> &lt;sub&gt;aaa&lt;/sub&gt; </root>
            – EZI
            Jul 14 '14 at 23:42












            Sorry, but, how do you know his xml is like "<root> <sub>aaa</sub> </root>"? Maybe his nodes are simplier than that.
            – MJVC
            Jul 15 '14 at 0:01




            Sorry, but, how do you know his xml is like "<root> <sub>aaa</sub> </root>"? Maybe his nodes are simplier than that.
            – MJVC
            Jul 15 '14 at 0:01












            Marcos, simpler or not, your code wouldn't work. It escapes and converts all innerXml to string. What don't you simply copy and paste my 3-lines code and test it with your solution. No need to discuss about which can be tested easily.
            – EZI
            Jul 15 '14 at 0:06






            Marcos, simpler or not, your code wouldn't work. It escapes and converts all innerXml to string. What don't you simply copy and paste my 3-lines code and test it with your solution. No need to discuss about which can be tested easily.
            – EZI
            Jul 15 '14 at 0:06














            Your 3 lines? Again, maybe his XML is simplier.
            – MJVC
            Jul 15 '14 at 0:08




            Your 3 lines? Again, maybe his XML is simplier.
            – MJVC
            Jul 15 '14 at 0:08




            5




            5




            Marcos, Come on, Please....., It would take your a few minutes. Take my codes, append your solution, open VS and test them with any xml you want. NO YOUR SOLUTION DOESN'T WORK
            – EZI
            Jul 15 '14 at 0:23






            Marcos, Come on, Please....., It would take your a few minutes. Take my codes, append your solution, open VS and test them with any xml you want. NO YOUR SOLUTION DOESN'T WORK
            – EZI
            Jul 15 '14 at 0:23




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24743916%2fhow-to-convert-xmlnode-into-xelement%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'