所以这是我的XML,我知道OrderDate,BuyerID和Items被称为子节点,但是你怎么称呼Item中的属性,比如ItemName,Category ect ..它们仍被称为子节点吗?如果是这样,它们应该被称为什么?
<?xml version="1.0" encoding="utf-8" ?>
<OrderData >
<Order OrderID="OR00001">
<OrderDate>26 May 2017</OrderDate>
<BuyerID>WCS1810001</BuyerID>
<Instructions>Place item carefully</Instructions>
<Items ItemID="IT00001">
<ItemName>ASUS Monitor</ItemName>
<Description>Best monitor in the world</Description>
<Category>Monitor</Category>
<Quantities>100</Quantities>
<Manufacturer>ASUS</Manufacturer>
<UnitPrice>$100.00</UnitPrice>
</Items>
</Order>
</OrderData>
答案 0 :(得分:0)
您使用的唯一属性(以xml术语表示)是OrderID和ItemID属性。使用xml时,只使用xml术语很有帮助。因此,xml中的其他所有内容都是**元素**。
另一个元素下的任何元素都是该元素的子元素。
Items是Order的子元素,ItemName是Items的子元素。
答案 1 :(得分:0)
为什么MS没有将它添加到C#中超出我的范围。 VB似乎更适合使用XML恕我直言。
Dim xe As XElement
' to load from a file
' Dim yourpath As String = "your path here"
'xe = XElement.Load(yourpath)
' for testing
xe = <OrderData>
<Order OrderID="OR00001">
<OrderDate>26 May 2017</OrderDate>
<BuyerID>WCS1810001</BuyerID>
<Instructions>Place item carefully</Instructions>
<Items ItemID="IT00001">
<ItemName>ASUS Monitor</ItemName>
<Description>Best monitor in the world</Description>
<Category>Monitor</Category>
<Quantities>100</Quantities>
<Manufacturer>ASUS</Manufacturer>
<UnitPrice>$100.00</UnitPrice>
</Items>
</Order>
</OrderData>
Dim item As XElement
'this does not find an item
item = (From el In xe...<Items>
Where el.@ItemID = "IT"
Select el).FirstOrDefault
If item Is Nothing Then Stop
'this finds the item
item = (From el In xe...<Items>
Where el.@ItemID = "IT00001"
Select el).FirstOrDefault
'add a new item to the order. an item prototype
Dim itmProto As XElement = <Items ItemID="">
<ItemName></ItemName>
<Description></Description>
<Category></Category>
<Quantities></Quantities>
<Manufacturer></Manufacturer>
<UnitPrice></UnitPrice>
</Items>
Dim newItem As New XElement(itmProto) 'note that itmProto is not used directly, only as part of New
newItem.@ItemID = "ITM0042"
newItem.<ItemName>.Value = "FOO"
newItem.<Description>.Value = "this is a test"
'etc
xe.<Order>.Last.Add(newItem) 'add to order
' to save file
' xe.Save(yourpath)