【问题标题】:C# XmlReader ReadElementContentAsString() InvalidOperationExceptionC# XmlReader ReadElementContentAsString() InvalidOperationException
【发布时间】:2016-07-01 04:42:02
【问题描述】:

我正在尝试读取特定的 XML,将必要的信息保存到 OrderOrderDetail 两个不同的类中。但是,当我尝试使用以下代码从我的 XML 中读取“BuyerID”时,它会抛出一个InvalidOperationExceptionReadElementContentAsString method is not supported on node type None. Line 1, Position 634http://pastebin.com/Lu4mKtwq

在这一行:

order.CustomerID = reader.ReadElementContentAsString();

我的源码:http://pastebin.com/JyTz8x0G

这是我正在使用的 XML:

文本中的 XML:

<Orders>
<Order ID="O2">
<OrderDate>1/7/2016</OrderDate>
<BuyerID>WSC1810</BuyerID>
<OrderItem>
<Item ID="R1">
<ItemName>8GB RAM King</ItemName>
<Decscription>8GB RAM King</Decscription>
<Capacity>8GB</Capacity>
<Quantities>150</Quantities>
<existingUnitPrice>100.00</existingUnitPrice>
</Item>
<Item ID="R2">
<ItemName>4GB RAM King</ItemName>
<Decscription>4GB RAM King Brand</Decscription>
<Capacity>4GB</Capacity>
<Quantities>100</Quantities>
<existingUnitPrice>50.00</existingUnitPrice>
</Item>
</OrderItem>
<RemarksandSpecialInstruction>Fragile, handle with care</RemarksandSpecialInstruction>
</Order>
</Orders>

我正在使用的课程:

【问题讨论】:

  • 您能否将 XML 输入也作为文本而不是图像发布?
  • 同样出于好奇,如果您的意图是将 XML 转换为对象,反之亦然,为什么不简单地使用序列化和反序列化而不是读取每个属性?

标签: c# xml invalidoperationexception


【解决方案1】:

ReadElementContentAsString方法读取当前节点后移动到下一个节点。 所以在你的情况下,有了下面的代码

reader.ReadToFollowing("OrderDate");
order.OrderDate = reader.ReadElementContentAsString();

现在代码已经在 OrderID 上,所以不要再尝试读取它了。 而是执行以下代码:

        while (reader.ReadToFollowing("Order"))
        {               
            order.OrderID = reader.GetAttribute("ID");
            string orderID = reader.Value;

            reader.ReadToFollowing("OrderDate");
            if(reader.Name.Equals("OrderDate"))
                order.OrderDate = reader.ReadElementContentAsString();
            if (reader.Name.Equals("BuyerID"))
                order.CustomerID = reader.ReadElementContentAsString();               
            orderList.Add(order);
            while (reader.ReadToFollowing("Item"))
            {
                OrderDetail i = new OrderDetail();
                i.OrderID = orderID;                  
                i.ItemID = reader.GetAttribute("ID");

                reader.ReadToFollowing("Decscription");
                if (reader.Name.Equals("Decscription"))
                    i.Description = reader.ReadElementContentAsString();
                if (reader.Name.Equals("Capacity"))
                    i.Capacity = reader.ReadElementContentAsString();
                if (reader.Name.Equals("Quantities"))
                    i.Quantity = reader.ReadElementContentAsInt();
                if (reader.Name.Equals("existingUnitPrice"))
                    i.AskingPrice = reader.ReadElementContentAsDecimal();
                orderDetailList.Add(i);
            }
        }

还要确保你的 xml 和你的模型是匹配的。

【讨论】:

    【解决方案2】:

    客户 ID 实际上被正确读取,它是引发异常的下面的行。 您的错误在这行代码上:

    reader.ReadToFollowing("Instructions");
    

    原因是xml不包含元素Instructions。 你可以通过不丢弃ReadToFollowing的结果来改进你的代码

    if (reader.ReadToFollowing("Instructions"))
        reader.ReadElementContentAsString();
    

    【讨论】:

    • 我把它改成了正确的元素名称,但它仍然指向同一个地方
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多