how to convert an XML into an array in asp.net - asp.net

i need to convert an XML into an array using asp.net.
I tried searching on the web but couldn't find the proper solution.
Can someone please help me with this
Thanks,
Alloi

I find LinqToXML helpful in these circumstances. I have a very simple XML document:
<?xml version="1.0" encoding="utf-8" ?>
<People>
<Person>
<id>1</id>
<name>Dave</name>
</Person>
<Person>
<id>2</id>
<name>Pete</name>
</Person>
<Person>
<id>3</id>
<name>Marie</name>
</Person>
</People>
And the following code that will turn it into an array of objects:
FileStream file = new FileStream("data.xml", FileMode.Open);
XDocument xmldoc = XDocument.Load(file);
var people = (from p in xmldoc.Descendants("Person")
select new
{
ID = p.Element("id").Value,
Name = p.Element("name").Value
}).ToArray();
foreach (var person in people)
{
Console.WriteLine(person.Name);
}

I think LinqToXML might be helpful in your situation. Here's a sample in vb.net (I could not find anything in csharp...): http://msdn.microsoft.com/en-us/vbasic/bb738047.aspx#convnodesarray

If you have an XSD to which the XML refers to, that probably can be a starting point on the implementation of array (or list).

A way... might help you
Read xml into dataset.
Iterate dataset on the basis of table rows and add each row entity to a list.
display list as array or list itself.

I don't know if there is a .net class that performs this action directly or not but in case there is no you have 2 options :
1 . Load XML into dataset then loop in the dataset and set elements of the array.
2 . Get XML nodes number then loop through XML and read each node element then put it in the array.
Hope that this is useful

Alloi - i think that due to the complexity inherent in xml structures, this will very much depend on the xml itself. what i'm saying is that a generic solution may be very difficult to make bomb proof, whereas a solution to a 'known' structure may be fairly straightfwd. the serialiser may allow you to save it to ToList() or similar but beyond that, it's a case of horses for sources i think. :)
jim
[edit] - link that may be useful: http://www.c-sharpcorner.com/UploadFile/chauhan_sonu57/SerializingObjects07202006065806AM/SerializingObjects.aspx
also - a nice little link showing how to convert xml->json in c# : http://www.phdcc.com/xml2json.htm

Related

XML XQuery basic querying results

I'm here with a question that I hope can be answered, which is really quite silly and basic.
I have a file of authors in the format of:
<authorRoot>
<author>
<info tags on author>
</author>
etc
</authorRoot>
and all I wish to do is, through FLWOR, return a list where each 'author' and its information is a different value, so when I run the query, the result should come out looking like
1. <author><info>.....</info></author>
2. <author><info>.....</info></author>
etc
and I am CERTAIN that something as simple as that should just be the following code
xquery version "1.0";
for $x in //author
return $x
yet when I do so, the query result comes out as
1.<author><info>...</info></author><author><info>...</info></author><author><info>...</info></author><author><info>...</info></author><author><info>...</info></author>....etc
I'm relatively new to XQuery, and I'm using AltovaSpy. I've done similar questions as basic as this (where I have a file of similar layout and I use essentially the same code, resulting in an xquery result page of multiple values, not just one long one) but for this file it just doesn't seem to work! Is it something with my code that I'm just not seeing? Or could it be the file, perhaps?
Thank you for whatever input you have on the situation.
Well, your reasoning is correct. .
It is just a formatting issue, it seems Altova prints the entire sequence in a single line without linebreaks.
You can also try it in my XQuery online tester, there you can see that the sequence is as you expected it to be.
If you watch this demo video of Altova XMLSpy and advance to 2:35 you will see how clicking on one of the toolbar buttons (which appears to be labeled "Pretty-print") will format the results of your XQuery as nicely indented XML.

how to get xml html after transpose and databind()

I have some code that uses xsl and xml.
The Xml control is on the design page.
The xml control id is xmlApplication
The xmlstring is generated and xsl has the format with all the tables and cells etc.
Here is a part of thecode of a page which generates the final product which shows the xml in a certain format.
xmlApplication.Document = xmlDoc;
xmlApplication.Transform = transApp;
xmlApplication.DataBind();
I am guessing after xmlApplication.Databind(), xmlApplication will be converted into something that can be put inside .
Is it possible to grab as a string?
Please let me know if I have a wrong idea abut this.
Thanks a lot.
http://www.logiclabz.com/c/net-c-function-to-convert-xml-document-into-html-string-using-xslt.aspx

Adding XML Attributes to a DataSet

I want to add an attribute to the dataset declared below whose value is the value of one of the field in the row.
So I want to add the id as shown below.
<root>
<Table id="GAS-405">
<apple>2009FA</apple>
<orange>3.00</orange>
<pear>BGPR</pear>
<banana>GAS-405</banana>
</Table>
</root>
This will help me identify the node later in my application.
Is this possible? Is this easier to do using XMLDocument?
Dim sdaFoo As SqlDataAdapter = New SqlDataAdapter("SELECT BLAH FROM BLAHBLAH", conn)
Dim dsFoo As DataSet = New DataSet()
dsFoo.DataSetName = "apple"
sdaFoo.Fill(dsFoo)
dsFoo.WriteXml("C:\Inetpub\wwwroot\foo.xml")
Dataset.WriteXml() is really a convenience method rather than a flexible way of dealing with XML.
You'll need to take another approach. There are a few options:
If you're just adding a single attribute, you could hack it into the resulting xml by re-opening the file as an XDocument, adding the attribute to the necessary element, and saving it again. Not too elegant, but easy, and sometimes easy is best. Even better, just use WriteXml() to put your xml into a string, then load the string as your XDocument.
Generate the XML from your query directly, rather than as a dataset. Sql Server 2005 and 2008 have some good XML methods that allow you to select a set of rows as XML (SELECT ... FOR XML) and specify what it looks like.
Use XmlSerialization for your dataset and inject the attribute using custom control of the serialization process.... which will be way more trouble than it's worth.
Store the attribute somewhere else outside of your XML and use some kind of object to keep track of it. Not really sure what your code is like, but that might be a great option.
Use GetXML() method of DataSet to get whole XML as a string. Then add your custom attributes and write that string into xml file using StreamWriter.

XQuery - remove nodes based on its sub element being in the "ban" list

I am a total noob with XQuery, but before at start digging deep into it, i'd like to ask some experts advice about whether i am looking at the correct direction.
The problem:
A huge xml file which contains a whole lot of users and their access information (password access rights and so on) example below:
<user>
<name>JC1234</name>
<password>popstar</password>
<accesslevel>0</accesslevel>
</user>
<user>
<name>AHkl</name>
<password>Rudy10!</password>
<accesslevel>2</accesslevel>
</user>
i have a list of user names (csv file) that i need to remove from that huge xml files.
the result should be a new xml file wihtout those removed users....
is this feasable with XQuery?
any advice for a quick and dirty solution is welcomed!
There is no standard way of loading a CSV file in vanilla XQuery 1.0, although most implementations have an unparsed-text function or similar. If not the contents of the file can be passed in as a parameter.
The CSV file can be parsed using the tokenize function:
declare variable $names = tokenize(unparsed-text("banned.csv"), ",")
And the actual query is quite straightforward. Assuming your document is a a fragment containing just a list of <user /> nodes then the query is simply
doc("users.xml")/user[not(name=$names)]
If however the XML file contains a lot of other data then you may find XSLT's templating facilities more useful.

XML Read specific Node

I want to read an specific xml node and its value for example
<customers>
<name>John</name>
<lastname>fetcher</lastname>
</customer>
and my code behind should be some thing like this (I don't know how it should be though):
Response.Write(xml.Node["name"].Value)
As I said it is just an example because I don't know how to do it.
The most basic answer:
Assuming "xml" is an XMLDocument, XMLNodeList, XMLNode, etc...
Response.Write(xml.SelectSingleNode("//name").innerText)
Which version of .NET are you using? If you're using .NET 3.5 and can use LINQ to XML, it's as simple as:
document.Descendant("name").Value
(except with some error handling!) If you're stuk with the DOM API, you might want:
document.SelectSingleNode("//name").InnerText
Note that this hasn't shown anything about how you'd read the XML in the first place - if you need help with that bit, please give more detail in the question.
If using earlier versions of the .Net framework, take a look at the XMLDocument class first as this is what you'd load the XML string into. Subclasses like XMLElement and XMLNode are also useful for doing some of this work.
haven't tried testing it but should point you in the right direction anyway
'Create the XML Document
Dim l_xmld As XmlDocument
'Create the XML Node
Dim l_node As XmlNode
l_xmld = New XmlDocument
'Load the Xml file
l_xmld.LoadXml("XML Filename as String")
'get the attributes
l_node = l_xmld.SelectSingleNode("/customers/name")
Response.Write(l_node.InnerText)

Resources