正在尝试打开此测试文件,该文件位于我的解决方案下的文件夹中:
C:\dev\trunk\Development\WebSvc\WCFProj\Xml\po.xml我的c#方法是:
public XmlDocument generateXmlResponse()
{
string appDir = AppContext.BaseDirectory;
XmlDocument xml = new XmlDocument();
xml.LoadXml(@"Xml\ResponseTempl.xml");
return xml;
}
异常消息是:
"Data at the root level is invalid. Line 1, position 1."现在,当我遍历代码时,我可以使用相对路径:
appDir + @"Xml\po.xml"它正确地解析为:
C:\dev\trunk\Development\WebSvc\WCFProj\Xml\po.xml我刚刚从微软网站https://msdn.microsoft.com/en-us/library/bb343181(v=vs.110).aspx上获取了样本PurchaseOrder.xml:
<PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">
<Address Type="Shipping">
<Name>Ellen Adams</Name>
<Street>123 Maple Street</Street>
<City>Mill Valley</City>
<State>CA</State>
<Zip>10999</Zip>
<Country>USA</Country>
</Address>
<Address Type="Billing">
<Name>Tai Yee</Name>
<Street>8 Oak Avenue</Street>
<City>Old Town</City>
<State>PA</State>
<Zip>95819</Zip>
<Country>USA</Country>
</Address>
<DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes>
<Items>
<Item PartNumber="872-AA">
<ProductName>Lawnmower</ProductName>
<Quantity>1</Quantity>
<USPrice>148.95</USPrice>
<Comment>Confirm this is electric</Comment>
</Item>
<Item PartNumber="926-AA">
<ProductName>Baby Monitor</ProductName>
<Quantity>2</Quantity>
<USPrice>39.98</USPrice>
<ShipDate>1999-05-21</ShipDate>
</Item>
</Items>
</PurchaseOrder>
发布于 2018-02-06 23:43:35
这里的问题是下面这行:
xml.LoadXml(@"Xml\ResponseTempl.xml");正在尝试将字符串加载为XML,并给出错误,因为它是无效的XML。LoadXml应该像这样使用:
xml.LoadXml("<item><name>wrench</name></item>");由于您正在尝试从文件中读取数据,因此需要使用:https://msdn.microsoft.com/en-us/library/875kz807(v=vs.110).aspx
这将如下所示:
xml.Load(@"Xml\ResponseTempl.xml");https://stackoverflow.com/questions/48646345
复制相似问题