?
Solved

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

Posted on 2014-09-03
1
Medium Priority
?
125 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 1000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

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