1

I have an XML File where i want to change the Attribute "subtype" in all the nodes with name "Product" to some other value. But somehow i seem to have problems with the select node part.

My XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<!-- GENERATED BY: PLM XML SDK 7.0.4.411 -->
<PLMXML xmlns="http://www.plmxml.org/Schemas/PLMXMLSchema" language="en-us" 
 time="16:26:35" schemaVersion="6" author="Teamcenter 
 V10000.1.0.70_20161101.00 - infodba@PESYS(1470348095)" date="2018-09-25">
  <Product id="id35" name="78033031000" subType="Item" accessRefs="#id10" 
   productId="78033031000">
   <Description>78033031000</Description>
   <ApplicationRef application="Teamcenter" label="JfYtyiXp1pzuzD" 
   version="JfYtyiXp1pzuzD"></ApplicationRef>
  </Product>
<Product id="id66" name="0405222613" subType="Item" accessRefs="#id10" 
  productId="0405222613">
  <Description>0405222613</Description>
  <ApplicationRef application="Teamcenter" label="yCdhv99n1pzuzD" 
  version="yCdhv99n1pzuzD"></ApplicationRef>
</Product>
<Product id="id90" name="59033080000" subType="Item" accessRefs="#id10" 
  productId="59033080000">
  <Description>59033080000</Description>
  <ApplicationRef application="Teamcenter" label="0OShlqVN1pzuzD" 
  version="0OShlqVN1pzuzD"></ApplicationRef>
</Product>
</PLMXML>

So my intuitive try looks like this:

    'Load the XML file in XmlDocument.
    Dim doc As New XmlDocument()
    doc.Load("C:\export\79533500000.xml")
    'Fetch the specific Nodes by Name.
    Dim nodeList As XmlNodeList = doc.SelectNodes("/PLMXML/Product")
    'Loop through the selected Nodes.
    For Each node As XmlNode In nodeList



        'Fetch the Node and Attribute values.
        node("Product").SetAttribute("subType", "Product_Value12")

    Next
    doc.Save("C:\export\test_new.xml")
End Sub

This actually does nothing so i tried to change doc.SelectNodes("/PLMXML/Product") to doc.SelectNodes("*") which does an attribute change for "subtype" but only for the first "Product" node.

Could you please help me an tell me what i am doing wrong here?

Thanks & Greets Bernhard

Bernsi
  • 25
  • 4
  • Best I could figure out was that the section `xmlns="http://www.plmxml.org/Schemas/PLMXMLSchema"` is causing problems. It tells the XmlDocument that you are following a particular schema, but you haven't told it what that schema looks like. If you can remove this part then I have an answer for you. – JayV Oct 02 '18 at 11:07
  • Hi, It is no problem to remove that part. – Bernsi Oct 02 '18 at 12:38

2 Answers2

0

What you missing is the XML Reader! Read it / change it / save it. Try it out:

Dim doc As XmlDocument = New XmlDocument()
Dim xmlReader As XmlTextReader = New XmlTextReader(yourFilePath)
doc.Load(xmlReader)
Dim nodes As XmlNodeList = doc.SelectNodes("PLMXML/Product")

For Each node As XmlNode In nodes

    If node.Attributes("subType").Value.ToString() = "Item" Then
        node.Attributes("subType").Value = "New_Item"
    End If
Next

xmlReader.Close()
doc.Save(yourFilePath)
Marco Sadowski
  • 436
  • 9
  • 19
  • Thanks for your input but unfortunately this also not seems to loop through the Product nodes, at least it does not change anything to the subtype attribute – Bernsi Oct 02 '18 at 10:35
0

If you remove this attribute it will work.

xmlns="http://www.plmxml.org/Schemas/PLMXMLSchema"

If you do need a namespace, then you'll have to declare it

    Dim nsmanager As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
    nsmanager.AddNamespace("x", "http://www.plmxml.org/Schemas/PLMXMLSchema")

    Dim nodeList As XmlNodeList = doc.SelectNodes("//x:Product", nsmanager)

This answer could help out with more details about this.

the_lotus
  • 12,668
  • 3
  • 36
  • 53