Solved

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

Posted on 2014-09-03
1
107 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
1 Comment
 
LVL 9

Accepted Solution

by:
s_chilkury earned 500 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now