【发布时间】:2016-04-13 20:40:37
【问题描述】:
我正在尝试解析 XML 并使用 XMLreader 从中获取所有信息。由于某种原因,它从标题中抛出了错误。这是xml:
<?xml version="1.0" encoding="utf-8" ?>
<queries>
<selectparent table="Associates">"SELECT * FROM @table"</selectparent>
<selectchild table="VehicleContract">"SELECT * FROM @table"</selectchild>
<addchild table="VehicleContract" read="contractId, regNr, assoc_id, percentage, vehicleType, trailerNr">"INSERT into @table (contractId, regNr, assoc_id, percentage, vehicleType, trailerNr) VALUES (@id, @reg, @assort, @perc, @type, @trailer)"</addchild>
<updatechild table="VehicleContract" read="contractId, regNr, assoc_id, percentage, vehicleType, trailerNr">"UPDATE @table SET regNr=@reg, assoc_id=@assort, percentage=@perc, vehicleType=@type, trailerNr=@trailer WHERE contractId=@id"</updatechild>
<removechild table="VehicleContract" read="id">"DELETE from @table WHERE contractId=@id"</removechild>
</queries>
还有我的方法:
public string selectParent, selectChildren, addChildren, updateChildren, removeChildren;
public string[] addParams, updateParams, removeParams;
XmlTextReader reader = new XmlTextReader(base.Path);
reader.WhitespaceHandling = WhitespaceHandling.None;
reader.Read();
reader.Read();
reader.Read();
selectParent = reader.ReadElementContentAsString();
reader.Read();
selectChildren = reader.ReadElementContentAsString();
addParams = reader.GetAttribute("read").Split(',');
addChildren = reader.ReadElementContentAsString();
updateParams = reader.GetAttribute("read").Split(',');
updateChildren = reader.ReadElementContentAsString();
removeParams = reader.GetAttribute("read").Split(',');
removeChildren = reader.ReadElementContentAsString();
我在这一行得到错误:
selectParent = reader.ReadElementContentAsString
有什么问题?我不断添加reader.Read(),因为我不断收到相同的错误,但在不同的节点类型上。
据我所知,table 属性应该替换查询中的@table...但是有些不对劲。有什么想法吗?
【问题讨论】:
-
您可能需要检查
base.Path是否引用了预期的 XML 文件。当我将 XML 作为字符串嵌入并使用new MemoryStream(Encoding.UTF8.GetBytes(xml))将其加载到阅读器中时,您的代码对我有用。此外,如果您可以调试代码并查看reader.NodeType和reader.Name,这将告诉您您在文件中的位置。 -
我使用了完整路径,在调试器中,它把我带到了 xml。当我到达那一行时,它显示 >"SELECT * FROM @table" 然后抛出错误:/