Save XML directly to Database with C# - c#

Here is a part of my xml file
<teams>
<team-profile>
<name>Australia</name>
<id>1</id>
<stats type="Test">
<span>1877-2010</span>
<matches>721</matches>
<won>339</won>
<lost>186</lost>
<tied>2</tied>
<draw>194</draw>
<percentage>47.01</percentage>
</stats>
<squad>
<player id="135" fullname="Shane Warne"/>
<player id="136" fullname="Damien Martyn"/>
<player id="138" fullname="Michael Clarke"/>
</squad>
</team-profile>
</team>
I have read somewhere that there is a way to save this XML directly to database. I am using VS2010. I have created the dataset, for the data i need from this xml. Is there any way to map this XML directly on dataset?
Any other idea?
I also have to save some other more complex XML files to database.
I have tried xsd.exe to create xsd schema for this XML.

SQL Server 2005+ has a native XML datatype, if you create a column, you can simply do an insert into that column, here is an insert similar to yours, this table's second column's datatype is xml
INSERT INTO docs VALUES (1, '<book genre="security"
publicationdate="2002" ISBN="0-7356-1588-2">
<title>Writing Secure Code</title>
<author>
<first-name>Michael</first-name>
<last-name>Howard</last-name>
</author>
<author>
<first-name>David</first-name>
<last-name>LeBlanc</last-name>
</author>
<price>39.99</price>
</book>')
INSERT INTO docs VALUES (2,
'<doc id="123">
<sections>
<section num="1"><title>XML Schema</title></section>
<section num="3"><title>Benefits</title></section>
<section num="4"><title>Features</title></section>
</sections>
</doc>')

Related

Nhibernate - xml column not well formed

I have some XML snippets that I store via NHibernate (3.2.0.4000) in a SQL Server database. The table column is defined as XML. Storing and Updating of the XML is working, but in the database I am missing the first node with the encoding and version information.
C# object, inner XML:
<?xml version="1.0" encoding="utf-16"?>
<RsSn>
<Layermapping xmlns="someNamespace">
<Layer layerName="0" layerDescription="5876548" />
<Layer layerName="1" layerDescription="5876549" />
</Layermapping>
</RsSn>
SQL Server Profiler when the data is inserted:
declare #p6 xml
set #p6=convert(xml,
N'<RsSn><Layermapping xmlns="someNamespace">
<Layer layerName="0" layerDescription="5876548"/>
<Layer layerName="1" layerDescription="5876549"/>
</Layermapping>
</RsSn>'
)
The <?xml version="1.0" encoding="utf-16"?> element is missing!
Content of SQL server column (via SSMS studio):
<RsSn>
<Layermapping xmlns="someNamespace">
<Layer layerName="0" layerDescription="5876548" />
<Layer layerName="1" layerDescription="5876549" />
</Layermapping>
</RsSn>
NHibernate Configuration (part of mapping file)
<class name="SnippetEntity" table="tblSnippet">
<id name="SnippetPK" column="SnippetPK" type="Int64">
<generator class="native" />
</id>
<property name="ResourceData" column="ResourceData" type="NHibernate.Type.XmlDocType"/>
</class>
I am not allowed to update the NHibernate assembly.
Is this the correct type that I am using in the mapping file?
Is there some other configuration I have to use so the document is stored "well formed" in the database?
The top <?xml version="1.0" encoding="utf-16"?> construct is called XML prolog. It is not mandatory to have it. XML is well-formed without it.
SQL Server stores XML as Unicode (utf-16). There is no other encoding. That's why SQL Server strips the prolog when it exists in XML.

Adding an XML file to your XNA project

I'm creating an XNA game. I've made it so I can specify all the level details in an XML file which is then de-serialized and used to set up the level details.
At the moment, it's just referencing a file on my computer - my question is, how do I reference this more generically?
Adding the xml in my content folder created a multitude of complaints about schemas and such like, which made me think that likely wasn't the correct route.
Any suggestions?
I tried removing all the entries from the XNA, this gives:
Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)
EDIT:
The xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XnaContent>
<Asset Type = "RDrop.Level[]">
<Item>
(stuff)
</Item>
<Item>
(stuff)
</Item>
</Asset>
</XnaContent>
EDIT:
I've started a new windows phone project - the previous one wasn't one. I've copied everything over and added "dataTypes" ala this tutorial:
http://msdn.microsoft.com/en-us/library/ff604979.aspx
Game project references -> content, MyDataTypes.
Content references -> MyDataTypes.
The XML is as is in previous edit and is contained in the content folder via Add-> Existing Item-> Level.XML.
Any ideas?
You can leave the build action as "Compile". One method to do what you want is the following:
Create a class that the xml is going to be describing. Example: Level.cs
Then structure your xml file like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XnaContent>
<Asset Type="The_Level_class_namespace.Level">
<Property1>Value</Property1>
<Property2>Value</Property2>
<Property3>Value</Property3>
<Property4>Value</Property4>
</Asset>
</XnaContent>
if you want the xml to describe an array of objects you can do structure the xml like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XnaContent>
<Asset Type="The_Level_class_namespace.Level[]">
<Item>
<Property1>Value</Property1>
<Property2>Value</Property2>
<Property3>Value</Property3>
<Property4>Value</Property4>
</Item>
</Asset>
</XnaContent>
From there you just need to make sure your values are in the proper format. For example a vector2 object would be like this:
<Vector2Property>x_value y_value</Vector2Property>
Make sure that your content project references the game project or library project.
Hope this helps :)
Open the properties of your XML document (right click in your content folder). You can set the Build Action to : None.
That way, the compiler won't analyse your schema, thus it won't produce any warnings.
(I'm not entirely sure about this, just my first guess)

Ill Formed XML Code

I currently try updating product data with the Amazon MWS and the Feeds API. My problem: Updating the Inventory and setting a new quantity for my products resolves in errors like this:
The XML you submitted is ill-formed at the Amazon Envelope XML level
at (or near) line X, column Y.
On the other hand, I export nearly the same XML to update the prices. That works just fine...
Here is an example of the XML that i upload to the Feeds API to update the quantity:
<?xml version="1.0" encoding="utf-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="amznenvelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>{SellerID}</MerchantIdentifier>
</Header>
<MessageType>Inventory</MessageType>
<Message>
<MessageID>1</MessageID>
<Inventory>
<SKU>ArtNoXX</SKU>
<Quantity>10</Quantity>
</Inventory>
</Message>
<Message>
<MessageID>2</MessageID>
<Inventory>
<SKU>ArtNoXY</SKU>
<Quantity>23</Quantity>
</Inventory>
</Message>
</AmazonEnvelope>
P.S.: I'm using C# and a XMLDocument to create the XML File...
Edit: The Error is shown multiple times. Only the first and the last 3 lines don't appear in the error log.
Example:
... (or near) line 10, column 16.
That would be
<Inventory>
Regarding to the column, it should be
>
Wrong namespace in your config ?
Yours :
noNamespaceSchemaLocation="amznenvelope.xsd"
Should be:
noNamespaceSchemaLocation="amzn-envelope.xsd"

C# XML Merging (2+ files)

I am having problem to merge 2 or more xml files into 1 using c#.
I am doing it with DataSets:
//ds1,ds2,ds3 are DataSets
private void MyMethod()
{
ds1.ReadXml(tmpStream);
ds2.ReadXml(tmpStream);
ds1.Merge(ds2);
}
but i dont want to use DataSet. i am searching for another way.
first XML is
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<item>
<path>'filePath'</path>
<deleted>0</deleted>
<date>9/23/2010 11:30:03 AM</date>
</item>
</catalog>
the second is
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<item>
<path>'filePath'</path>
<deleted>0</deleted>
<date>9/23/2010 11:30:03 AM</date>
</item>
</catalog>
result must be
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<item>
<path>'filePath'</path>
<deleted>0</deleted>
<date>9/23/2010 11:30:03 AM</date>
</item>
<item>
<path>'filePath'</path>
<deleted>0</deleted>
<date>9/23/2010 11:30:03 AM</date>
</item>
</catalog>
Though this isn't really clear of what sort of merge you want, this article Merging XML Files, Schema Validation, and More might help you get the idea.
Easiest could be, if you dont want any checks to be performed(duplicates, zombies, etc)
var ResultXml = XDocument.Load("file1.xml");
ResultXml.Root.Add(XDocument.Load("file2.xml").Root.Elements());
To merge XML files into resulting one, you could use Microsoft's XML Diff and Patch C# API. You could read more about it in Eric White's blog post: "OpenXmlDiff.Exe: A Utility to Find the Differences Between Two Open XML Documents"

How to read nested XML using xDocument in Silver light?

Hi currently I have a nested XMl , having the following Structure :
<?xml version="1.0" encoding="utf-8" ?>
<Response>
<Result>
<item id="something" />
<price na="something" />
<?xml version="1.0" encoding="UTF-8" ?>
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/">
</Result>
<NumberReturned>10</NumberReturned>
<TotalMatches>10</TotalMatches>
</Response>
Any help on how to read this using Xdocument or XMLReader will be really helpfull.
Thanks,
Subhendu
XDocument and XmlReader are both XML parsers that expect a properly formed XML as input. What you have shown is not a XML file. So the first task would be to extract the nested XML and as this is not valid XML you cannot rely on any parser to do this job. You'll need to resort to string manipulation and or regular expressions.
My suggestion would be to fix the procedure generating this invalid XML in the first place. Another suggestion is to never generate a XML file manually but use an appropriate tool for this (XmlWriter, XDocument, ...)

Categories