locked
ASP>Net4.5 C#: Ampersand Parsing Error RRS feed

  • Question

  • User-596225964 posted

    Dear all

    I'm very new at working with data from XML files.

    I am trying to display data from an xml file (MyXMLTest2.xml) which is a file from an external source so I cannot alter the content:

    <feed xmlns:urlEncoder="xalan://java.net.URLEncoder" xmlns="http://www.w3.org/2005/Atom">
      <title type="text">Holiday Rentals</title>
      <subtitle type="text">Holiday Rentals</subtitle>
      <id>
        https://partner.homeaway.eu/aggregator/london_prd_en/partners/search?partnerId=cj_uk&format=cjAtom&affiliateId=4584716
      </id>
      <link href="http://www.homeaway.co.uk"/>
      <link rel="self" type="application/atom+xml" href="https://partner.homeaway.eu/aggregator/london_prd_en/partners/search?partnerId=cj_uk&format=cjAtom&affiliateId=4584716"/>
      <link rel="next" type="application/atom+xml" href="https://partner.homeaway.eu/aggregator/london_prd_en/partners/search?partnerId=cj_uk&format=cjAtom&affiliateId=4584716&page=2"/>
      <link rel="first" type="application/atom+xml" href="https://partner.homeaway.eu/aggregator/london_prd_en/partners/search?partnerId=cj_uk&format=cjAtom&affiliateId=4584716&page=1"/>
      <link rel="last" type="application/atom+xml" href="https://partner.homeaway.eu/aggregator/london_prd_en/partners/search?partnerId=cj_uk&format=cjAtom&affiliateId=4584716&page=1577"/>
      <author>
        <name>HomeAway.com</name>
        <uri>http://www.homeaway.co.uk</uri>
      </author>
      <updated>2015-08-04T10:20:11.999Z</updated>
      <entry>
        <title type="text">
          Detached house for 10+ in Grasmere surrounded by spectacular views
        </title>
        <category term="category"/>
        <id>
          http://www.tkqlhce.com/click-4584716-10780684?URL=http%3A%2F%2Fwww.homeaway.co.uk%2Fp78887%3Fcid%3DL_CJ_4584716%26utm_source%3DCJ%26utm_medium%3Dpartners%26utm_campaign%3D4584716
        </id>
        <updated>2015-08-04T11:10:33.533</updated>
        <link rel="alternate" type="application/html" hreflang="en" href="http://www.tkqlhce.com/click-4584716-10780684?URL=http%3A%2F%2Fwww.homeaway.co.uk%2Fp78887%3Fcid%3DL_CJ_4584716%26utm_source%3DCJ%26utm_medium%3Dpartners%26utm_campaign%3D4584716"/>
        <summary>
          This magnificent Lakeland house stands in private gardens at the foot of Helvellyn, just a ten minute walk from the picturesque village of Grasmere in the heart of the national park. Bramrigg House can accommodate 10+ people, with 4 bedrooms upsta...
        </summary>
        <content type="xml">
          <listing systemId="trips" propertyId="78887" unitId="63823" url="http://www.tkqlhce.com/click-4584716-10780684?URL=http%3A%2F%2Fwww.homeaway.co.uk%2Fp78887%3Fcid%3DL_CJ_4584716%26utm_source%3DCJ%26utm_medium%3Dpartners%26utm_campaign%3D4584716" lastModified="2015-08-04T11:10:33.533" thumbnailUrl="http://imagesus.homeaway.co.uk/mda01/3ac90405-550d-4a38-bc81-681cb31f2aaf.1.3" imageUrl="http://imagesus.homeaway.co.uk/mda01/3ac90405-550d-4a38-bc81-681cb31f2aaf.1.6">
            <headline>
              Detached house for 10+ in Grasmere surrounded by spectacular views
            </headline>
            <description>
              This magnificent Lakeland house stands in private gardens at the foot of Helvellyn, just a ten minute walk from the picturesque village of Grasmere in the heart of the national park. Bramrigg House can accommodate 10+ people, with 4 bedrooms upsta...
            </description>
            <regions>
              <region>World</region>
              <region>Europe</region>
              <region>United Kingdom</region>
              <region>England</region>
              <region>Cumbria & Lake District National Park</region>
              <region>Windermere & Central Lakes</region>
              <region>Grasmere</region>
            </regions>
            <rates rentalBasis="property">
              <rate from="1000" to="2350" currencyUnit="GBP" periodType="weekly"/>
            </rates>
            <data propertyType="cottage" bedrooms="5" sleeps="10" wc="1" bathrooms="2" showerRooms="0" city="Grasmere" state="Cumbria" country="GB" postalCode="LA229RR">
              <availabilityRanges>
                <availabilityRange start="2015-08-03" end="2015-08-13" status="unavailable"/>
                <availabilityRange start="2015-08-21" end="2015-08-22" status="unavailable"/>
                <availabilityRange start="2015-08-28" end="2015-09-03" status="unavailable"/>
                <availabilityRange start="2015-09-11" end="2015-09-13" status="unavailable"/>
                <availabilityRange start="2015-09-25" end="2015-10-01" status="unavailable"/>
                <availabilityRange start="2015-10-23" end="2015-11-05" status="unavailable"/>
                <availabilityRange start="2015-11-20" end="2015-11-22" status="unavailable"/>
                <availabilityRange start="2015-12-04" end="2015-12-06" status="unavailable"/>
                <availabilityRange start="2015-12-27" end="2015-12-31" status="unavailable"/>
                <availabilityRange start="2016-01-01" end="2016-01-02" status="unavailable"/>
                <availabilityRange start="2016-01-22" end="2016-01-24" status="unavailable"/>
                <availabilityRange start="2016-02-12" end="2016-02-18" status="unavailable"/>
              </availabilityRanges>
            </data>
          </listing>
        </content>
      </entry>
      <entry>
        <title type="text">
          Apartment in in Dartmoor National Park - HAREL Short Breaks Available! – Please Enquire Now!
        </title>
        <category term="category"/>
        <id>
          http://www.tkqlhce.com/click-4584716-10780684?URL=http%3A%2F%2Fwww.homeaway.co.uk%2Fp1689403%3Fcid%3DL_CJ_4584716%26utm_source%3DCJ%26utm_medium%3Dpartners%26utm_campaign%3D4584716
        </id>
        <updated>2015-08-04T11:10:18.400</updated>
        <link rel="alternate" type="application/html" hreflang="en" href="http://www.tkqlhce.com/click-4584716-10780684?URL=http%3A%2F%2Fwww.homeaway.co.uk%2Fp1689403%3Fcid%3DL_CJ_4584716%26utm_source%3DCJ%26utm_medium%3Dpartners%26utm_campaign%3D4584716"/>
        <summary>
          1 OF 2 PURPOSE BUILT APARTMENTS WITH DARTMOOR VIEWS SLEEP 4 2 These delightful properties have been fitted and furnished to a very high standard and sit within the grounds of the owners friendly pub serving locally sourced produce and homemade foo...
        </summary>
        <content type="xml">
          <listing systemId="trips" propertyId="1689403" unitId="3263507" url="http://www.tkqlhce.com/click-4584716-10780684?URL=http%3A%2F%2Fwww.homeaway.co.uk%2Fp1689403%3Fcid%3DL_CJ_4584716%26utm_source%3DCJ%26utm_medium%3Dpartners%26utm_campaign%3D4584716" lastModified="2015-08-04T11:10:18.400" thumbnailUrl="http://imagesus.homeaway.co.uk/mda01/d198cc07-243d-4eac-a31e-8fd19f2addbd.1.3" imageUrl="http://imagesus.homeaway.co.uk/mda01/d198cc07-243d-4eac-a31e-8fd19f2addbd.1.6">
            <headline>
              Apartment in in Dartmoor National Park - HAREL Short Breaks Available! – Please Enquire Now!
            </headline>
            <description>
              1 OF 2 PURPOSE BUILT APARTMENTS WITH DARTMOOR VIEWS SLEEP 4 2 These delightful properties have been fitted and furnished to a very high standard and sit within the grounds of the owners friendly pub serving locally sourced produce and homemade foo...
            </description>
            <regions>
              <region>World</region>
              <region>Europe</region>
              <region>United Kingdom</region>
              <region>England</region>
              <region>West Country</region>
              <region>Cornwall</region>
              <region>South East Cornwall</region>
              <region>Gunnislake</region>
            </regions>
            <rates rentalBasis="property">
              <rate from="280" to="595" currencyUnit="GBP" periodType="weekly"/>
            </rates>
            <data propertyType="apartment" bedrooms="2" sleeps="4" wc="0" bathrooms="2" showerRooms="0" city="Tavistock" state="" country="GB" postalCode="PL19 8NT">
              <availabilityRanges>
                <availabilityRange start="2015-08-04" end="2015-08-21" status="unavailable"/>
                <availabilityRange start="2015-08-26" end="2015-08-30" status="unavailable"/>
              </availabilityRanges>
            </data>
          </listing>
        </content>
      </entry>
    </feed>

    This is my mark-up:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Styles/Main.Master" AutoEventWireup="true" CodeBehind="GetXML3.aspx.cs" Inherits="Pembs1.Pages.Restricted.GetXML3" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="cphHead" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="cphBody" runat="server">
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Data/MyXMLTest2.xml"></asp:XmlDataSource>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="XmlDataSource1" XPath="/Feed/Entry" AutoGenerateColumns="false" >
            <Columns>
            <asp:TemplateField HeaderText="Title" HeaderStyle-Width="300">
                <ItemTemplate>
                    <%# XPath("Title") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ID" HeaderStyle-Width="200">
                <ItemTemplate>
                    <%# XPath("ID") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Summary" HeaderStyle-Width="300">
                <ItemTemplate>
                    <%# XPath("Summary") %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        </asp:GridView>
    </asp:Content>

    I receive an xml parsing error which refers to the ampersand in line 5 (the link in the ID tag).

    From reading around it seems that ampersand is an xml escape character.  I quickly got lost in some of the explanations.

    I need to keep this data intact as I will be using these links that contain the ampersand characters.

    How do I get around this please so I can keep the data intact?

    Many thanks for your help.

    Jubbs

    Friday, January 22, 2016 3:21 AM

Answers

  • User614698185 posted

    Hi Jubbs,

    A literal ampersand inside an XML tag is not allowed by the XML standard, and such a document will fail to parse by any XML parser.

    You could replace & in xml with &amp;

    Regex.Replace can give you more control to avoid this scenario, and can be written to only match "&" symbols that are not part of other characters, such as &lt;, something like:

    string result = Regex.Replace(test, "&(?!(amp|apos|quot|lt|gt);)", "&amp;");

    And you could use HtmlEncode like below:

    string result = Regex.Replace(test, @"value=\""(.*?)\""", m => "value=\"" +
        HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(m.Groups[1].Value)) + "\"");
    var doc = XElement.Parse(result);
    

    For more information, please refer to the following link:

    https://support.microsoft.com/en-us/kb/316063

    http://stackoverflow.com/questions/1473826/parsing-xml-with-ampersand

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 6:00 AM

All replies

  • User614698185 posted

    Hi Jubbs,

    A literal ampersand inside an XML tag is not allowed by the XML standard, and such a document will fail to parse by any XML parser.

    You could replace & in xml with &amp;

    Regex.Replace can give you more control to avoid this scenario, and can be written to only match "&" symbols that are not part of other characters, such as &lt;, something like:

    string result = Regex.Replace(test, "&(?!(amp|apos|quot|lt|gt);)", "&amp;");

    And you could use HtmlEncode like below:

    string result = Regex.Replace(test, @"value=\""(.*?)\""", m => "value=\"" +
        HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(m.Groups[1].Value)) + "\"");
    var doc = XElement.Parse(result);
    

    For more information, please refer to the following link:

    https://support.microsoft.com/en-us/kb/316063

    http://stackoverflow.com/questions/1473826/parsing-xml-with-ampersand

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2016 6:00 AM
  • User-596225964 posted

    Thanks Candice

    Tuesday, February 2, 2016 2:06 AM