resthere
resthere

Reputation: 129

Xdocument throws error while XMLdocument loads sucessfully the same xml string

The existing Project loads the XMLDocument from a string transactionXML which is like

 \n\t<Transaction>
  \n\t\t<TransactionId>6109</TransactionId>
  n\t\t<TransactionType>ClaimsCorrespondenceEvent
 </TransactionType>\n\t\t<TransactionStatus>

The below code loads xml sucessfully:

   document.LoadXml(transactionXML);

but the below code fails and gives exception of invalid character referring to the \n\t etc.

     XDocument.Load(transactionXML);

I need to use XDocument as I am using the method Descendants of XDocument which is not available in XMLDocument.

Please is there a way to load the above string by using XDocument. Alternatively how do I use XMLdocument to get all the nodes along with the child with node name "Transaction"

The example XML would be :

<Transactions>
  <Transaction>
   <Version> 1 </Version>
   <Id> 2 </Id>
  </Transaction>
  <Transaction>
     <Version> 2 </Version>
     <Id> 3 </Id>
    </Transaction>
   <Transaction>
     <Version> 3 </Version>
     <Id> 4 </Id>
   </Transaction>
</Transactions>

and I want separate XML for the node Transaction along with its child elements like :

Ist XML set

    <Transaction>
     <Version> 1 </Version>
     <Id> 2 </Id>
    </Transaction>

2nd XMl set

  <Transaction>
     <Version> 2 </Version>
     <Id> 3 </Id>
    </Transaction>

3rd XMl set

   <Transaction>
     <Version> 3 </Version>
     <Id> 4 </Id>
   </Transaction>

Please if some one could let me know how to go about.

Upvotes: 0

Views: 710

Answers (1)

Ismail Hawayel
Ismail Hawayel

Reputation: 2453

For XDcoument, to load xml from a string, you need to use Parse not Load; Load accepts a file path and not an xml string, try:

var xml = XDocument.Parse(transactionXML);

http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx

Upvotes: 1

Related Questions