Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

How to use ReadXML to Extract Supervisor Information

$
0
0

Hi,

I am new to use ReadXML in C#.  I have partial code below which will read the XML data from XDocument then import them into SQL dataset.  I use XDocSupv.CreateReader method to convert XML format into dataset rows.  For some reason, I ran into multiple supervisor rows of Linda Lambert in the dataset and missing some of the rows that they should be in.  In the attached XML sample file, I have a James Davis and Susan Wong report to Linda Lambert who is the supervisor.  Notice that each employee has a supervisor field toward the end of each employee record.  Now, I get multiple rows Linda Lambert in my dataset.  I am expecting one row of each employee James, Susan, and Linda on the dataset result.  It looks like the supervisor at the end of each employee is overriding the employee.  Is there another way to write the ReadXML to read XDocument then parse it into dataset?

How should I change my dsSupv.ReadXML(XDocSupv.CreateReader()) to extract the XML file correctly?

Any suggestion would be appreciated.

<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><employee><uid>209</uid><firstName>James</firstName><lastName>Davis</lastName><middleName>E</middleName><nickName /><previousName>James  Davis</previousName><ssn>555-22-4444</ssn><employeeID>1234</employeeID><birthDate>1956-01-31T00:00:00</birthDate><gender>M</gender><username>Daviss</username><citizenshipCode /><citizenship /><maritalStatus>M</maritalStatus><maritalStatusDescr>Married</maritalStatusDescr><unionBargU>0</unionBargU><ethnicOrigin>Hispanic</ethnicOrigin><smoker>false</smoker><terminationDate xsi:nil="true" /><archived>false</archived><contact><country>USA</country><address1>1239 Booklyn Blvd</address1><address2 /><city>Booklyn</city><state>NY</state><zip>21023</zip><home_phone /><work_phone>(913) 333-5533</work_phone><email>Daviss@gmail.com</email><other_email>Daviss@yahoo.com</other_email></contact><issupervisor>true</issupervisor><supervisor><uid>963</uid><firstName>Linda</firstName><lastName>Lambert</lastName><middleName /><employeeID>12030</employeeID><birthDate xsi:nil="true" /><unionBargU xsi:nil="true" /><smoker>false</smoker><terminationDate xsi:nil="true" /><archived>false</archived><issupervisor>true</issupervisor></supervisor></employee><employee><uid>963</uid><firstName>Linda</firstName><lastName>Lambert</lastName><middleName /><nickName /><previousName /><ssn>444-44-6666</ssn><employeeID>12030</employeeID><birthDate>1957-04-10T00:00:00</birthDate><gender>F</gender><username>LambertL</username><citizenshipCode /><citizenship /><maritalStatus>M</maritalStatus><maritalStatusDescr>Married</maritalStatusDescr><unionBargU>0</unionBargU><ethnicOrigin>White (Not of Hispanic Origin)</ethnicOrigin><smoker>false</smoker><terminationDate xsi:nil="true" /><archived>false</archived><contact><country>USA</country><address1>1234 Broadway ST</address1><address2 /><city>Manhattan</city><state>NY</state><zip>10293</zip><home_phone>(913) 888-1212</home_phone><work_phone>(913) 777-1111</work_phone><email>Lambertc@yahoo.com</email><other_email>LLambert@gmail.com</other_email></contact><issupervisor>true</issupervisor><supervisor><uid>17899</uid><firstName>Jason</firstName><lastName>Allen</lastName><middleName /><employeeID>18239</employeeID><birthDate xsi:nil="true" /><unionBargU xsi:nil="true" /><smoker>false</smoker><terminationDate xsi:nil="true" /><archived>false</archived><issupervisor>true</issupervisor></supervisor></employee><employee><uid>582</uid><firstName>Susan</firstName><lastName>Wong</lastName><middleName>M</middleName><nickName /><previousName /><ssn>701-92-0293</ssn><employeeID>9483</employeeID><birthDate>1981-01-16T00:00:00</birthDate><gender>F</gender><username>Wonga</username><citizenshipCode /><citizenship /><maritalStatus>X</maritalStatus><maritalStatusDescr>Separated</maritalStatusDescr><unionBargU>0</unionBargU><ethnicOrigin>White (Not of Hispanic Origin)</ethnicOrigin><smoker>false</smoker><terminationDate xsi:nil="true" /><archived>false</archived><contact><country>USA</country><address1>5502 Spring Street</address1><address2 /><city>New York</city><state>NY</state><zip>10029</zip><home_phone>(920) 402-4123</home_phone><work_phone>(920) 612-3231</work_phone><email>Wonga@yahoo.com</email><other_email>Wonga123@yahoo.com</other_email></contact><issupervisor>true</issupervisor><supervisor><uid>963</uid><firstName>Linda</firstName><lastName>Lambert</lastName><middleName /><employeeID>12030</employeeID><birthDate xsi:nil="true" /><unionBargU xsi:nil="true" /><smoker>false</smoker><terminationDate xsi:nil="true" /><archived>false</archived><issupervisor>true</issupervisor></supervisor></employee></employees>

                     XDocument XDocSupv = new XDocument();
                     DataSet dsSupv = new DataSet();

                     dsSupv.ReadXml(XDocSupv.CreateReader());
                     DataTable dtSupv = dsSupv.Tables["Supervisor"];
                     using (SqlBulkCopy bc = new SqlBulkCopy(con))
                     {
                         bc.DestinationTableName = "Supervisor";
                         bc.ColumnMappings.Add("uid", "SupervisorID");
                         bc.ColumnMappings.Add("firstname", "First Name");
                         bc.ColumnMappings.Add("lastname", "Last Name");
                         bc.ColumnMappings.Add("employeeID", "EmployeeID");
                         bc.WriteToServer(dtSupv);
                     }



Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>