gpt4 book ai didi

c# - 使用 XmlReader c# 读取重复 block

转载 作者:行者123 更新时间:2023-11-30 23:24:44 24 4
gpt4 key购买 nike

我有以下 xml 文件:

<PackList>
<Header>
<OrderNumber>PO12123</OrderNumber>
<OrderQty>100</OrderQty>
</Header>
<Items>
<Item>
<PN>31023312</PN>
<Color>Black</Color>
</Item>
<Item>
<PN>22023312</PN>
<Color>White</Color>
</Item>
<Item>
<PN>44023312</PN>
<Color>Blue</Color>
</Item>
</Items>
</PackList>

我可以使用以下代码读取 Header 部分:

public class OrderItem
{
public string PN { get; set; }
public string Color { get; set; }
}

public class PList
{
public string OrderNumber { get; set; }
public int OrderQty { get; set; }
public List<OrderItem> OrderItems = new List<OrderItem>();
}

(...)
PList PL = new PList();
using (XmlReader reader = XmlReader.Create(@"c:/Test/PackList.xml"))
{
reader.ReadToFollowing("OrderNumber");
PList.OrderNumber = reader.ReadElementContentAsString();
reader.ReadToFollowing("OrderQty");
PList.OrderQty = reader.ReadElementContentAsInt();
}
(...)

但我不知道如何读取 Items 标签中的每个 Item

有什么建议吗?

最佳答案

reader.ReadToFollowing("OrderNumber");
plist.OrderNumber = reader.ReadElementContentAsString();
reader.ReadToFollowing("OrderQty");
plist.OrderQty = reader.ReadElementContentAsInt();

reader.ReadToFollowing("Items");

using (var innerReader = reader.ReadSubtree())
{
while (innerReader.ReadToFollowing("PN"))
{
var item = new OrderItem();
item.PN = innerReader.ReadElementContentAsString();
reader.ReadToFollowing("Color");
item.Color = innerReader.ReadElementContentAsString();

plist.OrderItems.Add(item);
}
}

reader.ReadToFollowing("foo");
var foo = reader.ReadElementContentAsString();

在哪里fooItems 之后的标签名称.

别忘了 plist.OrderItems = new List<OrderItem>();

关于c# - 使用 XmlReader c# 读取重复 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37595729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com