RE: LINQ to XML to Find Escaped Ampersand Problems?
Hi SnapDive,
Welcome to Microsoft MSDN newsgroup. My name is Lingzhi Sun [MSFT]. It is
my pleasure to work with you on this LINQ to XML question.
In LINQ to XML, the CDATA is represented by XCData
(http://msdn.microsoft.com/en-us/library/system.xml.linq.xcdata.aspx) and
the XML element?s inner text is expressed by XText
(http://msdn.microsoft.com/en-us/library/system.xml.linq.xtext.aspx). We
can use the XElement.DescendantNodesAndSelf
(http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.descendant
nodesandself.aspx) method to get all the XML nodes in an XML document.
Here are some code examples for your references:
Check the CDATA and Replace the value if necessary
======================================================================
XDocument doc = XDocument.Load("MyXml.xml");
var query = from element in doc.Root.DescendantNodesAndSelf()
where element.NodeType == System.Xml.XmlNodeType.CDATA
let CDataElement = element as XCData
where CDataElement.Value.Contains("&")
select CDataElement;
foreach (var element in query)
{
element.Value = element.Value.Replace("&", "&");
}
doc.Save("MyXml.xml");
======================================================================
Check the Inner Text and Replace the value if necessary
======================================================================
XDocument doc = XDocument.Load("MyXml.xml");
var query = from element in doc.Root.DescendantNodesAndSelf()
where element.NodeType == System.Xml.XmlNodeType.Text
let TextElement = element as XText
where TextElement.Value.Contains("&&")
select TextElement;
foreach (var element in query)
{
element.Value = element.Value.Replace("&&", "&");
}
doc.Save("MyXml.xml");
======================================================================
If you can use VB.NET on this LINQ to XML case, you can also consider using
the new VB.NET feature, XML literals to query the XML document. For
detail, please see http://msdn.microsoft.com/en-us/library/bb384460.aspx &
http://msdn.microsoft.com/en-us/library/bb687701.aspx.
Please note: if there is a namespace in your XML document, please refer to
Working with XML Namespace
(http://msdn.microsoft.com/en-us/library/bb387093.aspx). You may need to
do some modification on my code snippets to query the XML elements with
namespaces.
If you have any questions regarding this case, please feel free to let me
know.
Regards,
Lingzhi Sun
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.