Click here to Skip to main content
15,904,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have string in my program like
XML
<table1>
</table1>
<table2>
</table2>
<table3>
</table3>


now how to convert it in xml document format?

i can not use Loadxml command because it throws multiple root element error..please help me....
Posted
Updated 20-Oct-10 0:05am
v2
Comments
Andreas Gieriet 20-Oct-10 5:46am    
Hello Sugato Pal,
Please review your example and properly escape so that we see the real data - there seem to be missing data...
What is the required result type? Do you want to store it in a file or have an object to the root node?
Add some sample C# code that you have tried out so that one can identify what went wrong with your approach.
Cheers
Andi
Sugato Pal 20-Oct-10 8:06am    
yeah i know that for working with loadxml we need one tag here.I have written one web service which will fetch data from backend and send to client in xml doc format (with out newdataset tag) for that i have done folllowing steps:
1.fetching dataset from backend(say loDs is a dataset) 2.Use Getxml() to convert it in string xml document.
3.now i'm using loadxml an using this i'm getting
<newdataset>
<table1>
</table1>
<table2>
</table2>

Now i have to delete the new dataset tag so i'm taking firstchild.Innerxml to remove the Newdataset tag.But doing this the output i'm getting is in string xml format but i want it in Xml doc format..
4.so i have one string value which dont have any root element
(i.e <newdataset> )..So how i convert it in Xml doc format with C sharp code is there any other way instead of using Load Xml ..Please suggest somthing.I am terribly stuck.Time is running away.My client want this output data without the Newdataset tag.please help me....

You need to add a root element, like so:

XML
<root>
    <table1 />
    ...
</root>


After you do that, LoadXML should work just fine.
 
Share this answer
 
Comments
Hiren solanki 20-Oct-10 7:33am    
true answer downvoted, let me counter vote.
you need to add a root element to your string.

Every XML should have a root element or else it will give u an error.

VB
dim xmlString as string
xmlString="<RootTable>
             <table1>
             </table1>
             <table2>
             </table2>
            </RootTable>"


and if you cant get the root element in the string, then just append it to your string

VB
dim newString as string
newString="<table1>
</table1>
<table2>
</table2>
<table3>
</table3>"


then,
VB
dim myXmlString as string="<RootTable>" & newString & "</RootTable>


then

VB
dim xmlDoc as new xmlDocument
xmlDoc.LoadXML(myXmlString)


Now it should work!! :)
 
Share this answer
 
v3
Comments
Dalek Dave 21-Oct-10 10:18am    
Good Answer
yeah i know that for working with loadxml we need one <root> tag
here I have written one web service which willfetch data from backend and send to client in xml doc format (with out newdataset tag) for that i have done folllowing steps:

1.fetching dataset from backend(say loDs is a dataset)
2.Use Getxml() to convert it in string xml document.
3.now i'm using loadxml an using this i'm getting
XML
<NewDataSet>
<table1>
</table1>
<table2>
</table2>
</NewDataSet>


Now i have to delete the new dataset tag so i'm taking firstchild.Innerxml to remove the Newdataset tag.bt doing this the output i'm getting is in string xml format but i want it in Xml doc format..
4.so i have one string value which dont have any root element (i.e
XML
<NewDataSet>
</NewDataSet>


)..So how i convert it in Xml doc format with C sharp code is there any other way instead of using Load Xml ..Please suggest somthing.I am terribly stuck.Time is running away.My client want this output data without the Newdataset tag.please help me....
 
Share this answer
 
v2
Comments
Tarun.K.S 20-Oct-10 8:31am    
NewDataset tag is not a child but its a root element of the XML.if u delete that, then it will be an empty xml with no root element and can give an error.
Tarun.K.S 20-Oct-10 8:42am    
Well how about using the WriteXml?
check this link: http://www.vbforums.com/showthread.php?t=521251 and this: http://msdn.microsoft.com/en-us/library/system.data.dataset.writexml.aspx
I think i hav the solution..i have solved that problem just adding one line in my c sharp code.
myDatas.DocumentElement.SetAttribute("xmlns", "WebService");

It will add namespace with the
<NewDataSet>
tag..so no need to remove it..Thanks every body for the help..if any body has better solution then feel free to share here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900