If you use an appropriate class or library, they will do the escaping for you. Many XML issues are caused by string concatenation.
XML escape characters
There are only five:
" "
' '
< <
> >
& &
Escaping characters depends on where the special character is used.
The examples can be validated at the W3C Markup Validation Service.
Text
The safe way is to escape all five characters in text. However, the three characters "
, '
and >
needn't be escaped in text:
<?xml version="1.0"?>
<valid>"'></valid>
Attributes
The safe way is to escape all five characters in attributes. However, the >
character needn't be escaped in attributes:
<?xml version="1.0"?>
<valid attribute=">"/>
The '
character needn't be escaped in attributes if the quotes are "
:
<?xml version="1.0"?>
<valid attribute="'"/>
Likewise, the "
needn't be escaped in attributes if the quotes are '
:
<?xml version="1.0"?>
<valid attribute='"'/>
Comments
All five special characters must not be escaped in comments:
<?xml version="1.0"?>
<valid>
<!-- "'<>& -->
</valid>
CDATA
All five special characters must not be escaped in CDATA sections:
<?xml version="1.0"?>
<valid>
<![CDATA["'<>&]]>
</valid>
Processing instructions
All five special characters must not be escaped in XML processing instructions:
<?xml version="1.0"?>
<?process <"'&> ?>
<valid/>
XML vs. HTML
HTML has its own set of escape codes which cover a lot more characters.
Sounds like a job for LINQ to XML!
var vidDoc = XDocument.Parse(vidXml);
var rssDoc = XDocument.Parse(rssXml);
var videos = vidDoc.XPathSelectElements("/videos/video");
var rssItems = rssDoc.XPathSelectElements("/rss/channel/item");
var matches = videos.Join(
rssItems,
video => video.Element(XName.Get("code")).Value,
rssItem => rssItem.Element(XName.Get("code", "http://test.com")).Value,
(video, item) => new {video, item});
foreach (var match in matches)
{
var children = match.item.Elements()
.Where(child => child.Name.NamespaceName == "http://test.com" &&
child.Name.LocalName != "code");
foreach (var child in children)
{
//remove the namespace
child.Name = XName.Get(child.Name.LocalName);
match.video.Add(child);
}
}
vidDoc.Save(Console.Out);
The above solution assumes that the RSS document looks something like this:
<rss xmlns:ns="http://test.com" version="2.0">
<channel>
<item>
<title>AAA123</title>
<link>http://test.com/AAA123</link>
<pubDate>Sun, 26 Jul 2009 23:59:59 -0800</pubDate>
<ns:code>AAA123</ns:code>
<ns:type>Awesome</ns:type>
<ns:group>Wonderful</ns:group>
</item>
</channel>
</rss>
Best Solution
If you like XSLT, there's a nice merge script I've used before at: Oliver's XSLT page