tags:

views:

189

answers:

2

I am trying to create an XML file to conform to someones XSD this XSD allows for XHTML content within its description tag. I have cut short the method hierarchy but basically it does the following:

using System.Xml.Linq;

public static XDocument Create()
    {            
        XDocument doc = new XDocument(
          new XDeclaration("1.0", Encoding.UTF8.HeaderName, String.Empty),
            GetCourses()
            );

        //ValidatingProcess(@".\xcri_cap_1_1.xsd", doc.ToString());

        return doc;
    }

private static XElement GetCourses(XElement provider)
    {
        //datatbale dt generated here from sql
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            provider.Add(new XElement("course",
                  new XElement("identifier", dt.Rows[i]["QN_ID"]),
                  new XElement("title", dt.Rows[i]["LSC Descriptor"]),
                  new XElement("subject", dt.Rows[i]["category"]),
                  new XElement("description", dt.Rows[i]["Course_Overview"],
                    new XAttribute("Type", "Course Overview")),
                  //new XElement("description", dt.Rows[i]["Entry_Requirements"]),
                    //new XAttribute("Type", "Entry Requirements"),
                  new XElement("description", dt.Rows[i]["Course_Contents"],
                    new XAttribute("Type", "Course Contents")),
                  new XElement("description", dt.Rows[i]["Assessment"],
                    new XAttribute("Type", "Assessment")),
                  new XElement("description", dt.Rows[i]["Progression_Route"],
                    new XAttribute("Type", "Progression Route")),
                  new XElement("url", "http://www.nnc.ac.uk/CourseLeaflets/Leaflet2.aspx?qnid=" + dt.Rows[i]["QN_ID"]),
                    GetQualification(dt.Rows[i])//,
                    //GetPresentation(row)
                    ));
        }

        return provider;
    }

The fields like "Course_Overview", "Entry_Requirements" contain XHTML without URL encoding but when added to the XDocument it seems to automatically encode them so I end up with &lt;P&gt; instead of <P>. Is there anyway to stop this or am I going to have to use something instead of XDocument.

The reason I started with XDocument is the layout of the code can appear similar to the end result.

+1  A: 

This behavior is correct.

What you want is to actually parse the description HTML and insert that. Have a look at XElement.Parse().

I believe that the following MSDN Social question addresses your case pretty well.

Lucero
+1  A: 

Adding text content is treated as if you wanted text content, and not "inner XML".

I think you would be best converting the string of XML into a Document Fragment, and then adding that fragment (essentially the sring needs to be converted into nodes, and then add the nodes).

Use XElement.Parse to create the document fragment.

Richard
Thanks I thought that was the reasoning but didn't know the way around it.Although I have just discovered some li tags are missing their end tag so I am goign to have to run it through something to confirm it is xhtml.
PeteT