如何在C Sharp中将字符串转换为XML? [英] How to convert string to XML in C sharp?

查看:60
本文介绍了如何在C Sharp中将字符串转换为XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有字符串,如

 <   table1  > ; 
<  /table1  > 
<   table2  > 
<  /table2  > 
<   table3  > 
<  /table3  >  



现在如何将其转换为xml文档格式?

我不能使用Loadxml命令,因为它会引发多个根元素错误..请帮助我....

解决方案

您需要添加一个根元素,如下所示:

 <   root  > ; 
    <   table1    > 
    ...
<  /root  >  



之后,LoadXML应该可以正常工作.


您需要在字符串中添加根元素.

每个XML应该有一个根元素,否则会给你一个错误.

  dim  xmlString  as   string 
xmlString = "  



如果无法在字符串中获取根元素,则只需将其附加到字符串中

  dim  newString  as  字符串
newString = "  



然后,

  dim  myXmlString  as  string =  < RootTable>"& newString& "   dim  xmlDoc  as   xmlDocument
xmlDoc.LoadXML(myXmlString)



现在应该可以了!! :)


是的,我知道要使用loadxml,我们需要一个< root>标签
在这里,我编写了一个Web服务,该服务将从后端获取数据并以xml doc格式(不带newdataset标签)发送给客户端,因为我已经完成了以下步骤:

1.从后端获取数据集(比如说loD是数据集)
2.使用Getxml()将其转换为字符串xml文档.
3.现在我正在使用loadxml,并且正在使用 < NewDataSet > < table1 > < /table1 > < table2 > < /table2 > < /NewDataSet >



现在我必须删除新的数据集标签,以便我使用firstchild.Innerxml删除Newdataset标签.bt,这样做,我得到的输出是字符串xml格式,但是我希望它是Xml doc格式. br/> 4.so我有一个没有任何根元素的字符串值(即

 <   NewDataSet  > 
<  /NewDataSet  >  



)..因此,我如何使用C尖锐代码将其转换为Xml doc格式,还有其他方法,而不是使用Load Xml ..请提出建议.我陷入了困境.时间不多了.我的客户希望此输出数据不包含Newdataset标签.请帮助我....


I have string in my program like

<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....

解决方案

You need to add a root element, like so:

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



After you do that, LoadXML should work just fine.


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.

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

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



then,

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



then

dim xmlDoc as new xmlDocument
xmlDoc.LoadXML(myXmlString)



Now it should work!! :)


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

<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

<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....


这篇关于如何在C Sharp中将字符串转换为XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆