c# – XmlDocument.Load多个根元素
发布时间:2020-12-15 20:55:23 所属栏目:百科 来源:网络整理
导读:大家好我在需要加载xml时遇到问题 我的代码就像 Stream.Seek(0,SeekOrigin.Begin);xmlDoc.Load(Stream); //-- throw XmlException (multiple root elements in 14,22)xmlDoc.PreserveWhitespace = true; xml文件是 soapenv:Envelope xmlns:dummy="http://www
大家好我在需要加载xml时遇到问题
我的代码就像 Stream.Seek(0,SeekOrigin.Begin); xmlDoc.Load(Stream); //--> throw XmlException (multiple root elements in 14,22) xmlDoc.PreserveWhitespace = true; xml文件是 <soapenv:Envelope xmlns:dummy=" http://www.somedomian.com/dummybus/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header Id="IDH"> <dummy:authentication> <id>Unique</id> <userid>myuser</userid> </dummy:authentication> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> <wsu:Timestamp> <wsu:Created>2015-09-07T12:21:15</wsu:Created> <wsu:Expires>2015-09-08T12:21:15</wsu:Expires> </wsu:Timestamp> </wsse:Security> </soapenv:Header> <!--- This is line 14 and > is a column 22---> <soapenv:Body> <dummy:dummybus> <msg>X1</msg> </dummy:dummybus> </soapenv:Body> </soapenv:Envelope> <信封> tag是文件的根,为什么发送此错误以及如何读取我的文件? 我找到了有趣的东西 当我保存文件时,而不是将其保存在Stream中,完美加载 xmlDoc.Load(urlToFile); xmlDoc.PreserveWhitespace = true; 看来这个错误只发生在Stream中,为什么? 此方法将String中的xml转换为MemoryStream public class XmlUtil { public static MemoryStream GenerateStreamFromString(string s) { MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write(s); writer.Flush(); stream.Position = 0; return stream; } 解决方法
因为你是从流中读取的,所以当你调用xmlDoc.Load(Stream)时,我会认为它不在开头.在通话之前你在用流做什么?
如果您的流支持搜索,您可以尝试在呼叫之前回到其开头: Stream.Seek(0,SeekOrigin.Begin); xmlDoc.Load(Stream); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |