Replacing element containing mixed content in LINQ

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello all,
I need help in wrapping text nodes within an element containing mixed content using LINQ. I have been trying but havent been successful. Please see my code below:
<pre lang="x-xml <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document>
<paragraph>The quick brown fox<bold>jumped</bold>over the fence</paragraph>
</document>[/code]
Element <paragraph> has mixed content. The output I am looking for is below:
<pre lang="x-xml <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document>
<paragraph><ph>The quick brown fox</ph>
<bold><ph>jumped</ph></bold>
<ph>over the fence</ph></paragraph>
</document>
[/code]
The code I have so far:
<pre lang="x-c# XElement rootElement = XElement.Load("Test.xml");
foreach (XNode childNode in rootElement.Nodes())
{
Type nodeType = childNode.GetType();
if (nodeType.Name == "XText")
{
childNode.ReplaceWith(new XElement("ph", childNode.ToString()));
}
}[/code]
But this throws me an exception saying "Parent element is missing".
Greets,<br/>
<br/>

<br/>

View the full article
 
Back
Top