Parse This XML to object

For some reason this was voted down for being to broad by users who don’t know much about xml and objects. This was definitely not a broad question. This is XML and was converted into objects using the qualified answer.

I have the following XML and I am not sure how to parse it into an object. I am not familiar with this type of xml. What is my first step? I would like to parse it into an object.

<?xml version="1.0" encoding="UTF-8" ?> 
<DATA_PROVIDERS UID="Providers|REP" FORCE_REFRESH="FALSE" DATA_PROVIDER="" FORMATTED="FALSE" REFRESH="TRUE">
<DATA_PROVIDER NAME="Prov" SOURCE="Provider" DATE="11/18/2014" DURATION="9s" REFRESH="TRUE" CUBE="1">
  <COLUMN INDEX="0" ID="119" TYPE="String" FORMAT="">Prov ID</COLUMN> 
  <COLUMN INDEX="1" ID="118" TYPE="String" FORMAT="">Prov Name</COLUMN> 
  <COLUMN INDEX="2" ID="113" TYPE="String" FORMAT="">Address Info</COLUMN> 
  <COLUMN INDEX="3" ID="110" TYPE="String" FORMAT="">Enroll Status Code</COLUMN> 
  <COLUMN INDEX="4" ID="119" TYPE="String" FORMAT="">Phone</COLUMN> 
  <COLUMN INDEX="5" ID="110" TYPE="String" FORMAT="">Fax</COLUMN> 
  <COLUMN INDEX="6" ID="109" TYPE="String" FORMAT="">Provider Status</COLUMN> 
  <COLUMN INDEX="7" ID="150" TYPE="Date" FORMAT="m/d/yyyy h:mm:ss am/pm">Provider Start Date</COLUMN> 
<ROW>
  <CELL INDEX="0">004042111</CELL> 
  <CELL INDEX="1">CONTOSO West INC</CELL> 
  <CELL INDEX="2">1234 Random Rd. SOMECITY, ZZ 12345 9876</CELL> 
  <CELL INDEX="3">F</CELL> 
  <CELL INDEX="4">5555551234123</CELL> 
  <CELL INDEX="5">5555551234</CELL> 
  <CELL INDEX="6">F - Agency Action</CELL> 
  <CELL INDEX="7">5/31/2011 12:00:00 AM</CELL> 
  </ROW>
<ROW>
  <CELL INDEX="0">004011117</CELL> 
  <CELL INDEX="1">CONTOSO North INC</CELL> 
  <CELL INDEX="2">4321 Random Rd. SOMECITY, ZZ 12345 9876</CELL> 
  <CELL INDEX="3">F</CELL> 
  <CELL INDEX="4">5555551234123</CELL> 
  <CELL INDEX="5">5555551234</CELL> 
  <CELL INDEX="6">F - Agency Action</CELL> 
  <CELL INDEX="7">5/31/2011 12:00:00 AM</CELL> 
  </ROW>
 </DATA_PROVIDER>
</DATA_PROVIDERS>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you’re using Visual Studio 2012 (or greater) and you’re targeting .NET 4.5 (or greater) then Visual Studio can create a class for you that matches the XML:

  • Copy the XML to your clipboard
  • From within Visual Studio: Edit –> Paste Special –> Paste XML As Classes

Then you’ll need to serialize the data from XML to your newly created class to create a new object:

var myObject = LoadFromXmlString<DATA_PROVIDERS>(xmlData);

public static T LoadFromXmlString<T>(string xml)
{
  T retval = default(T);
  try
  {
    XmlSerializer s = new XmlSerializer(typeof(T));
    MemoryStream ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(xml));
    retval = (T)s.Deserialize(ms);
    ms.Close(); 
  }
  catch (Exception ex)
  {
    ex.Data.Add("Xml String", xml);
    throw new Exception("Error loading from XML string.  See data.", ex);
  }
  return retval;

}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x