Parsing this XML Object
For some reason this was rejected for being widespread among users who know little about xml and objects. This was definitely not a broad question. This is XML and has been converted to objects using a qualified answer.
I have the following XML and I'm not sure how to parse it into an object. I am not familiar with this type of xml. What's 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>
-five
source to share
1 answer
If you are using Visual Studio 2012 (or greater) and you are targeting .NET 4.5 (or greater), then Visual Studio can generate an XML conformant class for you:
- Copy XML to clipboard
- In Visual Studio: Edit -> Paste Special -> Paste XML as Classes
Then you will need to serialize the data from XML to the newly created class in order 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;
}
+7
source to share