下面是我的XML,我知道OrderDate、BuyerID和Items被称为子节点,但是如何将Items中的属性称为ItemName、Category等。它们还被称为子节点吗?如果是这样的话,它们应该被称为什么?
<?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>发布于 2017-06-17 19:01:21
在xml术语中,您拥有的惟一属性是OrderID和ItemID属性。在使用xml时,只使用xml术语会很有帮助。因此,xml中的其他所有内容都是一个**元素**。
位于另一个元素下的任何元素都是该元素的子元素。
Items是Order的子元素,ItemName是Items的子元素。
发布于 2017-06-17 19:33:22
我不明白为什么微软没有把这个添加到C#中。VB似乎更适合使用XML IMHO。
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)https://stackoverflow.com/questions/44603258
复制相似问题