Link to home
Start Free TrialLog in
Avatar of Sha1395
Sha1395

asked on

Xquery Xml document thru C#

Hey guru's

Am planning to write a C# class library file to read the XML doc

Eg:

  <?xml version="1.0" encoding="UTF-8" ?>
- <externalPerson_1>
- <!-- Person Level  0
  -->
  <emplId />
  <altEmplId>34534543</altEmplId>
  <birthDate>1978-12-25</birthDate>
  <dlaUserId>david.harrison</dlaUserId>
  <contractor>false</contractor>
- <!--  Contact Details
  -->
 </externalPerson_1>

i wanna store all the fields as an object and call this dll thru windows or console application and update the DB.

Any help much appreciated.
Avatar of Kalpesh Chhatrala
Kalpesh Chhatrala
Flag of India image

Is it necessary to use XQuery? If not, strongly recommend you to use LINQ - Language INtegrated Query.

http://msdn.microsoft.com/en-us/vcsharp/aa336746

Arun
Avatar of Sha1395
Sha1395

ASKER

Thans Arun,its not necessary to use XQuery but prefer to use XQuery to extract the objects.
ASKER CERTIFIED SOLUTION
Avatar of Sudhakar Pulivarthi
Sudhakar Pulivarthi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi there.

Hope you mean something like the code I wrote below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace ConsoleApplication1
{
    class Program
    {
        public class Person
        {
            public string altEmplId;
            public DateTime birthDate;
            public string dlaUserId;
            public bool contractor;
        }

        static void Main(string[] args)
        {
            var doc = new XPathDocument("Data.xml");
            XPathNavigator nav = doc.CreateNavigator();
            var expr = nav.Compile("externalPerson_1");
            XPathNodeIterator iterator = nav.Select(expr);
            var Persons = new List<Person>();
            foreach (XPathNavigator person in iterator)
            {
                var p = new Person();
                p.altEmplId = person.SelectSingleNode("altEmplId").Value;
                p.birthDate = person.SelectSingleNode("birthDate").ValueAsDateTime;
                p.dlaUserId = person.SelectSingleNode("dlaUserId").Value;
                p.contractor = person.SelectSingleNode("contractor").ValueAsBoolean;
                Persons.Add(p);
            }
            foreach (var person in Persons)
            {
                // code to store in database
            }
        }
    }
}

Open in new window

Avatar of Sha1395

ASKER

Thanks a lot Sudhakar,i am quite interested to work on XQuery.i will go thru the code and hope if you don't mind if ask any question from the code.
Your welcome Sha, We are pleased in helping our experts...
Happy working!!!
Avatar of Sha1395

ASKER

Hi Sudhakar thanks for your earlier help,actually i need one more help.

Scenario is, am getting the external value (thru Webservices) to my system.I have to grab the value from the field and update my Db.

Enclosed my Schema file for your view.

Please give me some advice to proceed

Thanks in Advance
Schema.xsd
Hi
>>grab the value from the field - Are u receiving the xml data from the client and want to parse through it and get the field and later update the info to the DB right?
Correct me if i am wrong. Pls Elobrate the input by example with dummy values.
Avatar of Sha1395

ASKER

You are right ,am getting the xml file also (attached here) and grab the value and update the DB.

File.xml
Avatar of Sha1395

ASKER

This is my WSDL file,am totally numb here.

What i suppose to do and how to get the value from webservices. thanks a lot for your help.

I couldn't able to upload my wsdl file with extension,so please change the file extension as wsdl.

Thanks
ImportExportServiceV2.txt
Hi,

You have to enhance the Employee object and construct the complete object for each employee details in the xml you received. The parsing for the fields can be done as u can see in the accepeted solution comment post. Once this contruction is done.
Now pass this object to a method which can construct the queries to insert  the data to respective tables.
If u struck in any field parsing from the xml let me know.
Note: Keep ur table structure in mind when ur creating
Avatar of Sha1395

ASKER

Thanks Sudhakar
Even i was trying to use your code in my console application and try to debug its not populate or reading the xml file.

Am quite stuck here.
File is on local disk and can open it manually?
Avatar of Sha1395

ASKER

yep the file in C:\File.xml, even i open the file thru Run command

but some how its not reading the file.
Avatar of Sha1395

ASKER

The File.xml (attached in my previous post) is the same file i used to test your code.
Avatar of Sha1395

ASKER

Hi Jacco,

Am extremely sorry i haven't noticed your code until yesterday,it was working really great.Some how i couldn't able to execute Sudhakar code.

your code work like a charm retrieve all the values.i don't know how to share the points...please let me know if you know.

Anyway thanks a ton!
Avatar of Sha1395

ASKER

Hi Sudhakar,

I need some help regarding my webservice ,am trying to pass login info and retrieve the value from my webservice.

But am getting error as well i couldn't able to call a method to pass the string.

You can find the post here : https://www.experts-exchange.com/questions/26857377/No-Constructor-defined.html

Please give me a help.