SOAP Response to DataTable in C# - c#

I have been searching and trying in many forms to pass an XML Response to a simple DataTable in the same way Excel does.
The XML is like:
<?xml version="1.0" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAPSDK4:Function xmlns:SOAPSDK4="http://www.externalwebservice.com/message/">
<Lists>
<ListCode>12345</ListCode>
<ListGroups>
<ListGroup>
<CodeGroup>ASDF</CodeGroup>
<DescriptionGroup>Example</DescriptionGroup>
</ListGroup>
</ListGroups>
<List>
<CodeList>ABC</CodeList>
<DescriptionList>Example List</DescriptionList>
<ListCategories>
<ListCategory>Type1</ListCategory>
<ListCategory>Type2</ListCategory>
</ListCategories>
<ListKinds>
<Kind>
<KindType>A</KindType>
<KindTarget>1</KindTarget>
<KindAttributes>
<KindAttribute1>A</KindAttribute1>
<KindAttribute2>B</KindAttribute2>
</KindAttributes>
</Kind>
</ListKinds>
</List>
</Lists>
</SOAPSDK4:Function>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
What Excel does could help me in what I need and that is to fill 4 tables with this data and its relations.
I have tried using DataSet.ReadXML but the problem with it is that for some reason this method generates 14 tables with the data being of no use because there is no relation at all.
Later I tried with DataTable.ReadXML but it gave me the problem "DataTable does not support schema inference from Xml." then tried to use DataTable.ReadXmlSchema(XML) and it gave an error with no root document, then I created an XSD with the command prompt and the two generated gave an error, finally I used Visual Studio to generate the XSD, it generated 4 XSD with no errors but no columns where created and no data was added with none of them.
I am out of ideas now. Any suggestions? Maybe not a DataTable with no normalized data (what Excel does) but another approach going directly with the XML?
Thank you.

My suggestion would be Linq.XML technique in C# 3. LINQ to XML provides an in-memory XML programming interface that leverages the .NET Language-Integrated Query (LINQ) Framework. LINQ to XML uses the latest .NET Framework language capabilities and is comparable to an updated, redesigned Document Object Model (DOM) XML programming interface.
There are few interesting quick relevant examples are available at http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

Related

converting xml data type in sql server to mongodb

Just wondering if there's a way to facilitate a procedure, where I have an xml document, no attributes, it's purely nodal, that is saved within a column of a table in sql server, then traverse through this column, and convert it to json, then take that final outcome, and dump it into mongodb?
It does seem like a tall order, but I feel like there must be a way where I can take each xml data object stored in this column, convert to json in c# using some library (I seen some out there that exists), but I really don't want the hassle of dumping them into a file. The process of the conversion and dumping into mongodb seems like it'd take a very very long time to parse and insert. So I was wondering if there's an alternative route that'd make my long journey less troublesome.
[EDIT: adding example]
Here's an example of what could be in a column as xml data in a sql server column:
<Person>
<FirstName><![CDATA[John]]></FirstName>
<LastName><![CDATA[Doe]]></LastName>
<Person>
Or sometimes, I could have a list of things:
<Cities>
<City>
<Name>Cleveland</Name>
<State>Ohio</Name>
</City>
<City>
<Name>San Francisco</Name>
<State>California</State>
</City>
</Cities>
Now, is there a way to take these examples, convert them to json, in a bulk way for each column containing xml, and then dump them into mongodb? Do I have to write out this process in c#? What's a good way of completing this task efficiently?

XML Data and Schemas

Ok, here is specific case scenario:
My application is going to receive some XML inputs. Then the application needs to render that XML input, as well as do some calculations after parsing data from that XML input.
The deal is, that the application is data agnostic. It's code cannot know details about XML data and format during design-time. So am making it the responsibility of calling client tool to send a schema associated with the XML data. Based on that schema, application will parse and understand XML data it will receive.
So, questions:
Can XML Schema specify any custom attributes that I may decide my application will need to parse data?
Will it be ok if corresponding node in XML data will not specify those attributes themselves?
While navigating in XML data, node by node, how can I using C# load corresponding attributes and values from XML schema?
Basically, I'll need such custom attributes in schema for various nodes - showInTable, isPrimary, graphable etc etc
Thanks for help.
The way around this I would say is to have a some fixed part of the schema, for data that will be there - even if it is nullable.
Then after that, get the XML to use some sort of <metadata> tags to allow you to capture any additional information. Like
<Customer>
<Name>Joe Bloggs</Name>
<Age>65</Age>
<Metadata key="Criminal History">Grand Theft Auto</Metadata>
<Metadata key="Favourite Colour">Blue</Metadata>
</Customer>
Metadata can be shared (if defined up front), with a minOccurs='0', maxOccurs='unbounded'.

XML attribute to indicate overwrite vs. preserve existing value

I could use a little guidance on working with XML.
I'm working on a several XML interop specifications which will be used to create or update data in our system. I'd like to provide a way for my customers to specify which elements they want to maintain via automation using the XML spec vs. manually updating the records after the fact. The quick back story to this is that our customers have upstream applications that can supply some but typically not all of the data required by our application so they will typically be sending us partial information for a record and then maintaining the rest in our app.
So assuming a simple XML:
<Data>
<Element1 />
<Element2 />
<Element3 />
</Data>
Customer A may supply element 1 and 2 but not element 3 so they would want to configure 3 to persist values between updates but for 1 and 2 to always overwrite with the new value.
Customer B may supply element 1, 2 and 3 and want to configure to always overwrite all 3 elements.
So I guess what I'm really looking for with Customer A is something like this:
<Data>
<Element1>data1</Element1>
<Element2>data2</Element2>
<Element3 overwriteExistingData="false"></Element3>
</Data>
I understand XML to a degree but have found it very easy to create an XML schema by simply creating C# class, doing some decorating with attributes if necessary and then generating a schema with xsd.exe and calling it a day.
Things that I'm unsure of are
- how to distinguish between a null value and something that the customer didn't have a value for
- the best way to handle these partial data situations where I'm not going to get a complete replacement record for every update.
I'm sure I am missing the boat to some degree but I really like the simplicity of having a class definition and telling the customer to give me XML that allows me to just type one line of code to deserialize the XML into my C# object.
Any guidance would be much appreciated

Can't import xml to dataset if xmlnode has attribute?

I am using the following codes to read xml file to a datagridview in c#:
newDataSet.ReadXml(filepath);
dataGridView3.DataSource = newDataSet;
dataGridView3.DataMember = "aaa";
And my xml file looks like this:
<root>
<aaa>
<Param_1>1</Param_1>
<Param_1>2</Param_1>
<Param_1>3</Param_1>
</aaa>
</root>
I can read the xml to dataset without any problems. Then I added some
attributes to the <Param> nodes so it becomes
<Param_1 size="2">1</Param_1>
The dataset can't show any xml data, does anyone knows why?
Also if I change my xml file to something like:
<root>
<Data_1>
<Name>aaa</Name>
<Params>
<Param_1>1</Param_1>
<Param_1>2</Param_1>
<Param_1>3</Param_1>
</Params>
</Data_1>
</root>
Is there still possible to use DataSet method to read them into a datagridview or I have to use something like linq?
If I have to, can someone show me how to do that using linq?
I suggest you to read data to xml document and bind to it using XmlDataSource, and not DataSet. And verify that structure is correct for binding. Looking at your comment (not at the edit by John, but rather at the comment under you question, there is no / symbol which should be in the closing tag: </Data_1>.
Or change structure to any that you wish, provided that it suits you for binding. After that you can read data:
DataSet ds = new DataSet();
ds.ReadXml("XMLFile1.xml", XmlReadMode.InferSchema);
Regarding Linq: you can start from reading Getting Started with LINQ in C#. But anyway you should not create complex xml structure - making it complex adds you a lot of work to deal with it.
DataSets are not a general-purpose mechanism for using XML. If the DataSet would not have produced the XML, then you cannot import that XML.

When should I use xml schemas(.xsd) when reading xml?

I have a XML file which holds configuration data for data sources, and the related queries held in 'dataseries' elements.
As I don't really need domain objects made up from the XML, rather just settings read and used to configure connections etc. I was wondering if there is any advantage in using the XML schema I have defined?
I am using LINQ to XML for reading my XML and initially thought it would be a good idea to use strongly typed XML.
Should I be using the .xsd or is it overkill?
A mock XML file:
<?xml version="1.0" encoding="utf-8" ?>
<datasource name=" Datasource" cache="true">
<database>
<connection>
<provider-name>sqlServer6.0</provider-name>
<source name="E5"
connectionString=""/>
</connection>
<update-interval>30</update-interval>
<minimum-update-interval>2</minimum-update-interval>
</database>
<dataseries name="" identifier="e5">
<graph-type></graph-type>
<query>
SELECT Period, Price
FROM PriceUS
WHERE Date = #date
</query>
</dataseries>
<dataseries name="" identifier="e52">
<graph-type></graph-type>
<query>
SELECT Period, Price
FROM PriceUS
WHERE Date = #date
</query>
</dataseries>
</datasource>
There are two levels of "correct" XML documents: well-formed and valid.
Well-formed means it conforms to XML spec, and valid means that it conforms to your schema. If and when you are accepting an XML document from a total stranger, it's usually a good idea to check the validity of the document before moving forward.
As you mentioned, XML schema could also be used to generate XML databinding entities. When you publish a service or a schema to the world or your client, the schema document could be used as a spec. The world or your client can then use the XSD file to validate or databind to XML documents that you exchange.
Technically, there are two formal schema types in XML. There's the original schema syntax, called a Document Type Definition (DTD), which is a holdover from the ancient SGML days. And then there is the W3C standard, XSD, which is more compatible with modern data.
The only reason I mention this is that you might receive an XML file that is described using a DTD and you might need to know how to deal with it.
But please, friends don't let friends create DTDs for new applications.
A schema is good when you want to validate the XML, for example, ensure that certain elements are present or have a limited set of values. A schema is also good if you will be doing a lot of work with the XML and it would be easier to convert the XML into C# objects -- in this case you can use the xsd.exe code generator to generate C# objects that can be marshaled and unmarshaled from the XML.

Categories