Solved

C# code to extract the information from XML and store them to database.

Posted on 2014-09-03
1
121 Views
Last Modified: 2015-01-30
Hi,

Here is the XML file which has 2 entry. Goal is to puch all the values in this XML file to a database.

1. All tags in the XML file will be column in database.
2. Code should read all the entry one by one and save the data within respective tags to the database.

Thanks.

XML file

<feed xmlns:d="test">
  <title type="text"></title>
  <entry>
    <id>uuid:dc683734-ae2c-4303-b183-3ef759072b1a;id=1064283</id>
    <title type="text"></title>
    <updated>2014-09-02T13:37:10Z</updated>
    <author /><content type="application/xml">
      <m:properties>
      <d:CompanyID m:type="Edm.Int32">964</d:CompanyID>
        <d:OSName>&lt;All&gt;</d:OSName>
        <d:DataRange>Data range 5/28/2014 - 8/24/2014      Report data last updated 8/25/2014</d:DataRange>
        <d:ProductName>Limited1</d:ProductName>
        <d:ProductVersion>10.3.9</d:ProductVersion>
        <d:ReleaseStatus>PreRelease</d:ReleaseStatus>
        <d:FailureModeGroupHeader>Top user failures</d:FailureModeGroupHeader>
        <d:FailureName>FailureName1.dll!Unloaded</d:FailureName>
        <d:FailureTypeName>Crash32</d:FailureTypeName>
        <d:Hits m:type="Edm.Double">332874</d:Hits>
        <d:BugID m:type="Edm.Int32">18887</d:BugID>
        <d:ArchName>oth</d:ArchName>
        <d:Arch_Hits>912</d:Arch_Hits>
        <d:ArchPercentage m:type="Edm.Double">1</d:ArchPercentage>
        <d:ArchAllOtherPercentage m:type="Edm.Double">0</d:ArchAllOtherPercentage>
      </m:properties>
    </content>
  </entry>
  <entry>
    <id>uuid:dc683734-ae2c-4303-b183-3ef759072b1a;id=1064290</id>
    <title type="text"></title>
    <updated>2014-09-02T13:37:10Z</updated>
    <author />
    <content type="application/xml">
      <m:properties>
        <d:CompanyID m:type="Edm.Int32">964</d:CompanyID>
        <d:OSName>&lt;All&gt;</d:OSName>
        <d:DataRange>Data range 5/28/2014 - 8/24/2014      Report data last updated 8/25/2014</d:DataRange>
        <d:ProductName>Limited1</d:ProductName>
        <d:ProductVersion>10.3.9</d:ProductVersion>
        <d:ReleaseStatus>PreRelease</d:ReleaseStatus>
        <d:FailureModeGroupHeader>Top user failures</d:FailureModeGroupHeader>
        <d:FailureName>FailureName2.dll!Unloaded</d:FailureName>
        <d:FailureTypeName>Crash32</d:FailureTypeName>
        <d:Hits m:type="Edm.Double">332874</d:Hits>
        <d:BugID m:type="Edm.Int32">18887</d:BugID>
        <d:ArchName>oth</d:ArchName>
        <d:Arch_Hits>912</d:Arch_Hits>
        <d:ArchPercentage m:type="Edm.Double">1</d:ArchPercentage>
        <d:ArchAllOtherPercentage m:type="Edm.Double">0</d:ArchAllOtherPercentage>
      </m:properties>
    </content>
  </entry>
</feed>
0
Comment
Question by:searchsanjaysharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 9

Accepted Solution

by:
s_chilkury earned 500 total points
ID: 40301548
The best way is to split the XML into simpler XML (node/record of one)


XDocument doc = XDocument.Load("your.xml");
var newXMLs = doc.Descendants("DOC")
                 .Select(d => new XDocument(new XElement("DATABASE", d)));
foreach (var newXML in newXMLs)
{
    newXML.Save("newfilename.xml");
}

And then, insert all those each xml file with iteration using the following code:

DataSet reportData = new DataSet();
reportData.ReadXml(Server.MapPath(”report.xml”));

SqlConnection conn = new SqlConnection(”conn_str”);
SqlBulkCopy bc = new SqlBulkCopy(conn);
bc.DestinationTableName = “table”;

//map Cols
bc.ColumnMappings.Add(”field1”, “node1”);
bc.ColumnMappings.Add(”field2”, “node2”);

conn.Open();

bc.WriteToServer(reportData.Tables[4]);
conn.Close();
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question