使用VB.NET将XmlElement添加到非根节点列表

时间:2016-07-28 21:49:09

标签: xml vb.net element xelement

我试图将新元素添加到XML文件中的现有元素列表中。我使用此代码启动元素:

   A  R  N  D  C  Q  E  G  H  I  L  K  M  F  P  S  T  W  Y  V  B  Z  X  *
A  4 -1 -2 -2  0 -1 -1  0 -2 -1 -1 -1 -1 -2 -1  1  0 -3 -2  0 -2 -1  0 -4
R -1  5  0 -2 -3  1  0 -2  0 -3 -2  2 -1 -3 -2 -1 -1 -3 -2 -3 -1  0 -1 -4
N -2  0  6  1 -3  0  0  0  1 -3 -3  0 -2 -3 -2  1  0 -4 -2 -3  3  0 -1 -4
D -2 -2  1  6 -3  0  2 -1 -1 -3 -4 -1 -3 -3 -1  0 -1 -4 -3 -3  4  1 -1 -4
C  0 -3 -3 -3  9 -3 -4 -3 -3 -1 -1 -3 -1 -2 -3 -1 -1 -2 -2 -1 -3 -3 -2 -4
Q -1  1  0  0 -3  5  2 -2  0 -3 -2  1  0 -3 -1  0 -1 -2 -1 -2  0  3 -1 -4
E -1  0  0  2 -4  2  5 -2  0 -3 -3  1 -2 -3 -1  0 -1 -3 -2 -2  1  4 -1 -4
G  0 -2  0 -1 -3 -2 -2  6 -2 -4 -4 -2 -3 -3 -2  0 -2 -2 -3 -3 -1 -2 -1 -4
H -2  0  1 -1 -3  0  0 -2  8 -3 -3 -1 -2 -1 -2 -1 -2 -2  2 -3  0  0 -1 -4
I -1 -3 -3 -3 -1 -3 -3 -4 -3  4  2 -3  1  0 -3 -2 -1 -3 -1  3 -3 -3 -1 -4
L -1 -2 -3 -4 -1 -2 -3 -4 -3  2  4 -2  2  0 -3 -2 -1 -2 -1  1 -4 -3 -1 -4
K -1  2  0 -1 -3  1  1 -2 -1 -3 -2  5 -1 -3 -1  0 -1 -3 -2 -2  0  1 -1 -4
M -1 -1 -2 -3 -1  0 -2 -3 -2  1  2 -1  5  0 -2 -1 -1 -1 -1  1 -3 -1 -1 -4
F -2 -3 -3 -3 -2 -3 -3 -3 -1  0  0 -3  0  6 -4 -2 -2  1  3 -1 -3 -3 -1 -4
P -1 -2 -2 -1 -3 -1 -1 -2 -2 -3 -3 -1 -2 -4  7 -1 -1 -4 -3 -2 -2 -1 -2 -4
S  1 -1  1  0 -1  0  0  0 -1 -2 -2  0 -1 -2 -1  4  1 -3 -2 -2  0  0  0 -4
T  0 -1  0 -1 -1 -1 -1 -2 -2 -1 -1 -1 -1 -2 -1  1  5 -2 -2  0 -1 -1  0 -4
W -3 -3 -4 -4 -2 -2 -3 -2 -2 -3 -2 -3 -1  1 -4 -3 -2 11  2 -3 -4 -3 -2 -4
Y -2 -2 -2 -3 -2 -1 -2 -3  2 -1 -1 -2 -1  3 -3 -2 -2  2  7 -1 -3 -2 -1 -4
V  0 -3 -3 -3 -1 -2 -2 -3 -3  3  1 -2  1 -1 -2 -2  0 -3 -1  4 -3 -2 -1 -4
B -2 -1  3  4 -3  0  1 -1  0 -3 -4  0 -3 -3 -2  0 -1 -4 -3 -3  4  1 -1 -4
Z -1  0  0  1 -3  3  4 -2  0 -3 -3  1 -1 -3 -1  0 -1 -3 -2 -2  1  4 -1 -4
X  0 -1 -1 -1 -2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -2  0  0 -2 -1 -1 -1 -1 -1 -4
* -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4  1

但是我收到一条错误消息,说我无法使用" /"字符。不知道如何解决这个问题?

更新: 我添加了两个图像,第一个是XML文件的视图和数据所在的位置。文件:OriginalXML 蓝色项中突出显示的是我想要添加的内容(不必位于列表的顶部,附加数据可能更好)。 DisplayName,MultiMedia和CustomData作为简单的CDATA文本。 第二个文件是我运行代码时发生的事情。 (AfterCode.jpg)。请注意突出显示的"选项"似乎是在错误的地方,格式化丢失。 如果要查看原始XML文件,请执行以下操作: http://www.kwhdesign.ca/Temp/paf.xml

![OriginalXML] http://www.kwhdesign.ca/Temp/OriginalXML.jpg

(我无法发布超过2个链接,因此我会在另一条评论中尝试第二张图片)

希望这有帮助! 再次感谢!

2 个答案:

答案 0 :(得分:0)

创建元素,然后使用AppendChild

将其添加到您想要的位置
Dim newElem As XmlElement = doc.CreateElement("Option")
doc.SelectSingleNode("/Content/Catalog/Feature[7]").AppendChild(newElem)

<强>更新

目前还不完全清楚你想要做什么,但如果你想在最后一个<Option>之后插入一个新的<Option>元素,那么这样的事情将是一个开始虽然我认为它显示了这将是多么乏味(虽然它完全有可能有更简单的方法来生成新选项)!

Dim newOption = doc.CreateElement("Option")
newOption.SetAttribute("code", "ZZZ")
Dim displayName = doc.CreateElement("DisplayName")
displayName.SetAttribute("lang", "en-US")
displayName.AppendChild(doc.CreateCDataSection("This is a test"))
newOption.AppendChild(displayName)
Dim feature = doc.SelectSingleNode("/Content/Catalog/Feature[7]")
Dim lastOption = feature.SelectSingleNode("Option[last()]")
feature.InsertAfter(newOption, lastOption)

与@dbasnett一样,我更喜欢XDocument / XElement和LINQ to XML。添加一个新的<Option>将是这样的(注意索引在这里是0,与XPath不同):

Dim doc = XDocument.Load("paf.xml")
doc.<Content>.<Catalog>.<Feature>(6).<Option>.Last().AddAfterSelf(
  <Option code="ZZZ">
    <DisplayName lang="en-US"><![CDATA[This is a test]]></DisplayName>
    <Multimedia type="MediumImage"><![CDATA[Multimedia\Pictures\Medium\SherwinWilliams.jpg]]></Multimedia>
    <Multimedia type="LargeImage"><![CDATA[Multimedia\Pictures\Large\SherwinWilliams.jpg]]></Multimedia>
    <CustomData><![CDATA[<xml>
  <RECKEY code="010201650004001V" />
  <VARProp VarID="1237" PropID="Expression" Expr="SHERW" />
  <VARProp VarID="3518" PropID="Expression" Expr="SHERW" />
  <VARProp VarID="3580" PropID="Expression" Expr="SHERW" />
  <VARProp VarID="2149" PropID="VariableType" Expr="104" />
</xml>]]></CustomData>
  </Option>
)

答案 1 :(得分:0)

我更喜欢使用xelement来操作XML。这是一个使用xelement的示例。它有几个调试语句,因此您可以看到每个步骤的内容。希望它有所帮助。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim xe As XElement
    'to load from a file
    '  xe = XElement.Load("Your Path Here")

    'for testing you can do this
    xe = <content>
             <Catalog>
                 <feature></feature>
                 <feature></feature>
                 <feature></feature>
                 <feature></feature>
             </Catalog>
         </content>

    'to add an element to the base

    Debug.WriteLine("")
    Debug.WriteLine(xe.ToString)

    xe.AddFirst(New XElement("foo"))

    Debug.WriteLine("")
    Debug.WriteLine(xe.ToString)

    'to add an element to sub element
    Dim optProto As XElement = <option></option>
    Dim addthis As New XElement(optProto) 'note New
    xe.<Catalog>...<feature>.ElementAt(3).Add(addthis)

    Debug.WriteLine("")
    Debug.WriteLine(xe.ToString)

    addthis.Value = "added" 'note

    Debug.WriteLine("")
    Debug.WriteLine(xe.ToString)

End Sub
相关问题