2

am trying to convert the XML to Json Conversion by applying the xslt transformation using following code in C#. Am getting following error can any one suggest me on below? Code:

 Li = p.GetRegisterEntry();
        var std = Li.Where(s => s.Id == id).FirstOrDefault();
        string xml = std.contentxml.Value;
        doc.LoadXml(xml);            
        string XSLT = std.TemplateXSLT.Value;
        Xslt.LoadXml(XSLT);            
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(Xslt.CreateNavigator());           
        // Transform our Xml-ified JSON
        var outputDocument = new XmlDocument();
        var stream = new MemoryStream();
        xslt.Transform(doc, null, stream);
        var sr = new StreamReader(stream);
        var myStr = sr.ReadToEnd();
        stream.Position = 0;
        outputDocument.Load(stream);
        // Convert back to JSON
        string jsonText = JsonConvert.SerializeXmlNode(outputDocument);

am getting the following error at outputDocument.Load(stream); An exception of type 'System.Xml.XmlException' occurred in System.Xml.dll but was not handled in user code Additional information: There are multiple root elements. Line 2, position 2.

can any one please help me to crack this?

here are my XML and XSTL Files XML:

   <NewDataSet>
      <RegisterEntry type="CM2" desc="Request to Change Name, Address and /or Singapore Address for Service for Agent, Applicant/Proprietor and/or other interested Parties">
        <EventDate>23/09/2051</EventDate>
        <DataItems>
          <LodgementDate>26/08/2022</LodgementDate>
          <DecisionDate>21/12/2031</DecisionDate>
          <Particulars>
            <Particular>
              <TransactionType>Test Data</TransactionType>
              <Details>Test Data</Details>
            </Particular>
          </Particulars>
        </DataItems>
      </RegisterEntry>
    </NewDataSet>

XSLT :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" />
  <xsl:template match="RegisterEntry">
    <p>      Test Data Parties<xsl:value-of select="DataItems/TransactionType" /></p>
    <div id="content">
      <table>
        <tr>
          <th valign="top" align="left">            Lodgement Date          </th>
          <td>
            <xsl:value-of select="DataItems/LodgementDate" />
          </td>
        </tr>
        <tr>
          <th valign="top" align="left">            Decision Date          </th>
          <td>
            <xsl:value-of select="DataItems/DecisionDate" />
          </td>
        </tr>
        <xsl:for-each select="DataItems/Particulars/Particular">
          <tr>
            <th valign="top" align="left" style="width:50%">    Test Data <xsl:value-of select="TransactionType" /></th>
            <td>
              <xsl:value-of select="Details" />
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </div>
  </xsl:template>
</xsl:stylesheet>
5
  • either your XML document or XSLT document is invalid XML. Commented Jun 27, 2017 at 7:14
  • 2
    Can you edit your question to include a sample of the XML and XSLT transform that generate the exception - i.e. a minimal reproducible example? If we can run the code ourselves it is more likely we can help. But the error There are multiple root elements. Line 2, position 2. indicates that one of your XML documents has more than one root element. An XML document must have exactly one root element. Commented Jun 27, 2017 at 7:15
  • @brijber i think its not so the xml can be convertible to HTML using the same XSLT fileand the html file is proper. Commented Jun 27, 2017 at 7:18
  • 2
    @user3214322 - It is almost certainly the output document you are generating does not have a single root element, but to know for certain you should really edit your question to show your input XML and your XSLT. Thank you. Commented Jun 27, 2017 at 7:47
  • @TimC added the files as requested please help me Commented Jun 27, 2017 at 8:36

1 Answer 1

3

As Tim has pointed out, the problem is that your XSLT does not create a well-formed XML document you could load with XmlDocument but only a fragment. So if you need an XmlNode with the result you can use a different approach with e.g.

XmlDocument resultDoc = new XmlDocument();
XmlDocumentFragment resultFrag = resultDoc.CreateDocumentFragment();

using (XmlWriter xw = resultFrag.CreateNavigator().AppendChild())
{
  xslt.Transform(doc, null, xw);
  xw.Close();
}

string jsonText = JsonConvert.SerializeXmlNode(resultFrag);

Whether your libray JsonConvert is able to handle a document fragment I don't know however.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.