How to convert XmlNode into XElement?
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
add a comment |
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
Are all of yourXmlNode
instancesXmlElement
instances?
– John Saunders
Jul 14 '14 at 19:19
1
@JohnSaunders Yes, all theXmlNode
instances areXmlElement
instances.
– Alexander Abakumov
Jul 14 '14 at 19:36
1
@JohnSaunders, Sorry, not all theXmlNode
instances areXmlElement
instances. They might be, for example,XmlComment
instances.
– Alexander Abakumov
Jul 15 '14 at 9:04
add a comment |
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
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
c# .net linq linq-to-xml
edited Jan 3 '16 at 23:19
asked Jul 14 '14 at 19:05
Alexander Abakumov
4,38744067
4,38744067
Are all of yourXmlNode
instancesXmlElement
instances?
– John Saunders
Jul 14 '14 at 19:19
1
@JohnSaunders Yes, all theXmlNode
instances areXmlElement
instances.
– Alexander Abakumov
Jul 14 '14 at 19:36
1
@JohnSaunders, Sorry, not all theXmlNode
instances areXmlElement
instances. They might be, for example,XmlComment
instances.
– Alexander Abakumov
Jul 15 '14 at 9:04
add a comment |
Are all of yourXmlNode
instancesXmlElement
instances?
– John Saunders
Jul 14 '14 at 19:19
1
@JohnSaunders Yes, all theXmlNode
instances areXmlElement
instances.
– Alexander Abakumov
Jul 14 '14 at 19:36
1
@JohnSaunders, Sorry, not all theXmlNode
instances areXmlElement
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
add a comment |
5 Answers
5
active
oldest
votes
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
1
Thanks for the great solution usingXElement.Load( xmlElement.CreateNavigator().ReadSubtree() );
! This is perfect if You need to convert to XElement instances of theXmlElement
only. But for anXmlComment
instanceReadSubtree()
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 theNodeType
of all yourXmlNode
s is theElement
.
– Alexander Abakumov
Jul 15 '14 at 9:30
add a comment |
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);
}
3
Why not use OuterXml instead of InnerXml?
– Jenny O'Reilly
Aug 7 '15 at 7:09
add a comment |
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;
}
add a comment |
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
.
1
The question is about convertingXmlNode
, notXNode
.
– Alexander Abakumov
Nov 17 '17 at 21:18
add a comment |
As far as I know, you can do this:
XElement xdoc = new XElement(node.Name, node.InnerXml);
Absolutely no, this will escape to whole xml and you will get a single node like<root> <sub>aaa</sub> </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
|
show 5 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
1
Thanks for the great solution usingXElement.Load( xmlElement.CreateNavigator().ReadSubtree() );
! This is perfect if You need to convert to XElement instances of theXmlElement
only. But for anXmlComment
instanceReadSubtree()
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 theNodeType
of all yourXmlNode
s is theElement
.
– Alexander Abakumov
Jul 15 '14 at 9:30
add a comment |
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
1
Thanks for the great solution usingXElement.Load( xmlElement.CreateNavigator().ReadSubtree() );
! This is perfect if You need to convert to XElement instances of theXmlElement
only. But for anXmlComment
instanceReadSubtree()
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 theNodeType
of all yourXmlNode
s is theElement
.
– Alexander Abakumov
Jul 15 '14 at 9:30
add a comment |
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
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
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 usingXElement.Load( xmlElement.CreateNavigator().ReadSubtree() );
! This is perfect if You need to convert to XElement instances of theXmlElement
only. But for anXmlComment
instanceReadSubtree()
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 theNodeType
of all yourXmlNode
s is theElement
.
– Alexander Abakumov
Jul 15 '14 at 9:30
add a comment |
1
Thanks for the great solution usingXElement.Load( xmlElement.CreateNavigator().ReadSubtree() );
! This is perfect if You need to convert to XElement instances of theXmlElement
only. But for anXmlComment
instanceReadSubtree()
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 theNodeType
of all yourXmlNode
s is theElement
.
– 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 XmlNode
s 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 XmlNode
s is the Element
.– Alexander Abakumov
Jul 15 '14 at 9:30
add a comment |
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);
}
3
Why not use OuterXml instead of InnerXml?
– Jenny O'Reilly
Aug 7 '15 at 7:09
add a comment |
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);
}
3
Why not use OuterXml instead of InnerXml?
– Jenny O'Reilly
Aug 7 '15 at 7:09
add a comment |
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);
}
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);
}
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
add a comment |
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
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
edited Nov 21 '18 at 14:51
answered Nov 21 '18 at 14:36
Gustavo Lemos
11
11
add a comment |
add a comment |
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
.
1
The question is about convertingXmlNode
, notXNode
.
– Alexander Abakumov
Nov 17 '17 at 21:18
add a comment |
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
.
1
The question is about convertingXmlNode
, notXNode
.
– Alexander Abakumov
Nov 17 '17 at 21:18
add a comment |
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
.
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
.
answered Nov 17 '17 at 19:12
Mark Moore
225
225
1
The question is about convertingXmlNode
, notXNode
.
– Alexander Abakumov
Nov 17 '17 at 21:18
add a comment |
1
The question is about convertingXmlNode
, notXNode
.
– 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
add a comment |
As far as I know, you can do this:
XElement xdoc = new XElement(node.Name, node.InnerXml);
Absolutely no, this will escape to whole xml and you will get a single node like<root> <sub>aaa</sub> </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
|
show 5 more comments
As far as I know, you can do this:
XElement xdoc = new XElement(node.Name, node.InnerXml);
Absolutely no, this will escape to whole xml and you will get a single node like<root> <sub>aaa</sub> </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
|
show 5 more comments
As far as I know, you can do this:
XElement xdoc = new XElement(node.Name, node.InnerXml);
As far as I know, you can do this:
XElement xdoc = new XElement(node.Name, node.InnerXml);
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> <sub>aaa</sub> </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
|
show 5 more comments
Absolutely no, this will escape to whole xml and you will get a single node like<root> <sub>aaa</sub> </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> <sub>aaa</sub> </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> <sub>aaa</sub> </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
|
show 5 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Are all of your
XmlNode
instancesXmlElement
instances?– John Saunders
Jul 14 '14 at 19:19
1
@JohnSaunders Yes, all the
XmlNode
instances areXmlElement
instances.– Alexander Abakumov
Jul 14 '14 at 19:36
1
@JohnSaunders, Sorry, not all the
XmlNode
instances areXmlElement
instances. They might be, for example,XmlComment
instances.– Alexander Abakumov
Jul 15 '14 at 9:04