且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用.Net将字符串转换为XML

更新时间:2022-03-21 18:09:11

这段代码对我来说没问题.我认为问题出在您的xml数据上.
一些建议:

1.)您可以确认XML格式正确吗?您是否尝试过使用IE打开XML文件并确保没有错误?
2.)确保根元素前没有空格.
The piece of code looks ok to me. I think the problem is with your xml data.
A few suggestions:

1.) Can you confirm that the XML is well formed? Have you tried to open the XML file using IE and ensuring that there are no errors?
2.) Make sure there is no blank space before the root element.


我认为我注意到的是变量不匹配.将"s1"传递给LoadXML()方法时,您可以将XML字符串化为"s"变量.

您只需将"s"的输出保存在XML文件中,就可以确保从字符串(即本例中的变量"s")生成XML.

希望对您有所帮助.
One think I have noticed is the variable mismatch. You have XML to string in "s" variable while you are passing "s1" to the LoadXML() method.

You can ensure the XML generation from string (i.e. from variable "s" in this case), by simply saving the output of "s" in a XML file.

Hope it helps.


考虑到Manav的观点,您一定错过了XML声明.

试试这个:
字符串s =<?xml版本= \" 1.0 \?>" + objServicel.HelloWorld();

现在加载:xd.LoadXml(s);

现在工作了吗?
Considering Manav''s points, you must have missed the XML Declaration.

try this:
string s = "<?xml version = \"1.0\"?>" + objServicel.HelloWorld();

Now load : xd.LoadXml(s);

Did it work now?