Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I am little bit new to write xml file using vb.net, i want to write below attached xml file but after lot of try, i am not able to write this xml, pls. help me

Regards!
Raj

the xml formate is like below and want to write how ?

XML
<?xml version="1.0" encoding="UTF-8"?>
-<object-type xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="APP_TR_ERROR">
-<bd-objects>
    -<bd-object>
        -<fields>
            <field field-id="apptrIBuyerName">PVH</field>
            <field field-id="apptrTestCompanyName">INTERNAL</field>
            <field field-id="apptrNo">TR-0000000340</field>
            <field field-id="apptrErrorOccurDate">2014-03-11T10:11:23+00:00</field>
        </fields>
    </bd-object>
-<bd-subobjects>
-<object-type type="APP_TR_ERROR_DETAIL" parent-type="APP_TR_ERROR">
-<bd-objects>
    -<bd-object>
        -<fields>
            <field field-id="apptrErrorCode">103</field>
            <field field-id="apptrErrorCodeName">MISSING_MANDATORY_FIELDS</field>
            <field field-id="apptrErrorCodeDescription">Some mandatory fields (apptrScheduledDate, apptrEstCompletionDate) are missing.</field>
        </fields>
    </bd-object>
</bd-objects>
</object-type>
</bd-subobjects>
</bd-objects>
</object-type>


Edit:
VB
Dim doc As New XDocument(New XDeclaration("1.0", "UTF-8", String.Empty),
New XElement("Product",
New XElement("<object-type xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="APP_TR_ERROR">",
New XAttribute("apptrIBuyerName", "PVH"),
New XElement("apptrTestCompanyName", "lbs"),
New XElement("apptrNo", "tr001")),
New XElement("<object-type type="APP_TR_ERROR_DETAIL" parent-type="APP_TR_ERROR">",
New XAttribute("apptrErrorCode", "02"),
New XElement("apptrErrorCodeName", "d001"),
New XElement("apptrErrorCodeDescription", "error value"))))
doc.Save("D:\TestL.xml")
Posted
Updated 31-Jan-15 9:29am
v2
Comments
ZurdoDev 30-Jan-15 14:51pm    
Where are you stuck?
Sergey Alexandrovich Kryukov 30-Jan-15 15:49pm    
What have you tried so far?
—SA
CHill60 30-Jan-15 21:05pm    
Post the code that you have tried to make work and tell us what is the problem with it

1 solution

try this code here, it writes the first part fine, it is a little cumbersome but you can see how vb works with xml, the xmlwriter may be a better solution for you but this should get you going.


VB
Public Function customXmlDoc() As XmlDocument

       Dim xdoc As New XmlDocument
       Dim dec As XmlDeclaration = xdoc.CreateXmlDeclaration("1.0", "utf-8", "yes")
       xdoc.AppendChild(dec)
       Dim ns As New XmlNamespaceManager(xdoc.NameTable)
       ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")

       Dim rootElement As XmlElement = xdoc.CreateElement("rootElement")
       xdoc.AppendChild(rootElement)

       Dim firstChild As XmlElement = xdoc.CreateElement("object-type")
       Dim rootAtt As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "type", xdoc.GetNamespaceOfPrefix("xsi"))
       rootAtt.Value = "APP_TR_ERROR"
       firstChild.Attributes.Append(rootAtt)
       rootElement.AppendChild(firstChild)

       Dim objects As XmlElement = xdoc.CreateElement("bd-objects")
       firstChild.AppendChild(objects)
       Dim _object As XmlElement = xdoc.CreateElement("bd-object")
       objects.AppendChild(_object)

       Dim fields As XmlElement = xdoc.CreateElement("fields")
       _object.AppendChild(fields)

       Dim field As XmlElement = xdoc.CreateElement("field")
       field.InnerText = "PVH"
       Dim att As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       att.Value = "apptrIBuyerName"
       field.Attributes.Append(att)
       fields.AppendChild(field)


       Dim field1 As XmlElement = xdoc.CreateElement("field")
       field1.InnerText = "INTERNAL"
       Dim att1 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       att1.Value = "apptrTestCompanyName"
       field1.Attributes.Append(att1)
       fields.AppendChild(field1)

       Dim field2 As XmlElement = xdoc.CreateElement("field")
       field2.InnerText = "TR-0000000340"
       Dim att2 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       att2.Value = "apptrNo"
       field2.Attributes.Append(att2)
       fields.AppendChild(field2)

       Dim field3 As XmlElement = xdoc.CreateElement("field")
       field3.InnerText = "2014-03-11T10:11:23+00:00"
       Dim att3 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       att3.Value = "apptrErrorOccurDate"
       field3.Attributes.Append(att3)
       fields.AppendChild(field3)

       Return xdoc

   End Function



ok, I will post this as an improved solution below


VB
Public Function customXmlDoc() As XmlDocument

       Dim xdoc As New XmlDocument
       Dim dec As XmlDeclaration = xdoc.CreateXmlDeclaration("1.0", "utf-8", "yes")
       xdoc.AppendChild(dec)
       Dim ns As New XmlNamespaceManager(xdoc.NameTable)
       ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")





       'create root element
       'forgot to add the namespace string here
       Dim rootElement As XmlElement = xdoc.CreateElement("object-type", ns.LookupNamespace(("xsi")))
       Dim rootAtt As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "type", xdoc.GetNamespaceOfPrefix("xsi"))
       rootAtt.Value = "APP_TR_ERROR"
       rootElement.Attributes.Append(rootAtt)
       'append root element
       xdoc.AppendChild(rootElement)

       'look at your xml and see if this is the second "root type" element
       Dim secondRoot As XmlElement = xdoc.CreateElement("bd-objects")
       rootElement.AppendChild(secondRoot)

       'this is like the first row
       Dim firstRow As XmlElement = xdoc.CreateElement("bd-object")
       secondRoot.AppendChild(firstRow)

       ' this is the first child of the first row
       Dim fields As XmlElement = xdoc.CreateElement("fields")
       firstRow.AppendChild(fields)

       'here is a child of the first child of the first row
       Dim field As XmlElement = xdoc.CreateElement("field")
       field.InnerText = "PVH"
       Dim att As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       att.Value = "apptrIBuyerName"
       field.Attributes.Append(att)
       fields.AppendChild(field)

       'here is a child of the first child of the first row
       Dim field1 As XmlElement = xdoc.CreateElement("field")
       field1.InnerText = "INTERNAL"
       Dim att1 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       att1.Value = "apptrTestCompanyName"
       field1.Attributes.Append(att1)
       fields.AppendChild(field1)

       'here is a child of the first child of the first row
       Dim field2 As XmlElement = xdoc.CreateElement("field")
       field2.InnerText = "TR-0000000340"
       Dim att2 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       att2.Value = "apptrNo"
       field2.Attributes.Append(att2)
       fields.AppendChild(field2)
       'here is a child of the first child of the first row
       Dim field3 As XmlElement = xdoc.CreateElement("field")
       field3.InnerText = "2014-03-11T10:11:23+00:00"
       Dim att3 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       att3.Value = "apptrErrorOccurDate"
       field3.Attributes.Append(att3)
       fields.AppendChild(field3)

       'this wraps up the first row
       'start the next row

       'sub Objects

       'create the second row
       Dim secondRow As XmlElement = xdoc.CreateElement("bd-subobjects")
       'append the second row to the first row (the same as the first row) the second root
       secondRoot.AppendChild(secondRow)

       'first child of the second row next
       Dim firstChildRow2 As XmlElement = xdoc.CreateElement("object-type")
       'append to second row
       secondRow.AppendChild(firstChildRow2)
       Dim attType2 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "type", xdoc.GetNamespaceOfPrefix("xsi"))
       'append the attribute
       firstChildRow2.Attributes.Append(attType2)
       'give the attribute a value
       attType2.Value = "APP_TR_ERROR_DETAIL"
       Dim attType3 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "parent-type", xdoc.GetNamespaceOfPrefix("xsi"))
       'append the attribute
       firstChildRow2.Attributes.Append(attType3)
       'give the attribute a value
       attType3.Value = "APP_TR_ERROR"

       'first child of the first child of the second row next
       Dim secondRowFirstChild As XmlElement = xdoc.CreateElement("fields")
       'append to first child of row two
       firstChildRow2.AppendChild(secondRowFirstChild)

       'first child of the first child of the first child of the second row next
       Dim secondRowFirstChildFirstChild As XmlElement = xdoc.CreateElement("field")
       'set the field inner text
       secondRowFirstChildFirstChild.InnerText = "103"
       'append to first child of row two
       secondRowFirstChild.AppendChild(secondRowFirstChildFirstChild)
       'create attribute
       Dim att21 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       'append the attribute
       secondRowFirstChildFirstChild.Attributes.Append(att21)
       'give the attribute a value
       att21.Value = "apptrErrorCode"

       'here is the next item
       'second child of the first child of the first child of the second row next
       Dim secondRowFirstChildSecondChild As XmlElement = xdoc.CreateElement("field")
       'set the field inner text
       secondRowFirstChildSecondChild.InnerText = "MISSING_MANDATORY_FIELDS"
       'append to first child of row two
       secondRowFirstChild.AppendChild(secondRowFirstChildSecondChild)
       'create attribute
       Dim att22 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       'append the attribute
       secondRowFirstChildSecondChild.Attributes.Append(att22)
       'give the attribute a value
       att22.Value = "apptrErrorCodeName"

       'here is the next Item
       'second child of the first child of the first child of the second row next
       Dim secondRowFirstChildThirdChild As XmlElement = xdoc.CreateElement("field")
       'set the field inner text
       secondRowFirstChildThirdChild.InnerText = "MISSING_MANDATORY_FIELDS"
       'append to first child of row two
       secondRowFirstChild.AppendChild(secondRowFirstChildThirdChild)
       'create attribute
       Dim att23 As XmlNode = xdoc.CreateNode(XmlNodeType.Attribute, "field-id", xdoc.GetNamespaceOfPrefix("xsi"))
       'append the attribute
       secondRowFirstChildThirdChild.Attributes.Append(att23)
       'give the attribute a value
       att23.Value = "apptrErrorCodeName"

       Return xdoc

   End Function
 
Share this answer
 
v2
Comments
the other paul 31-Jan-15 17:40pm    
try the latest code here, I ran it and it looks to me like it writes the file that you need. The layout of your xml is a bit confusing, if this file is generated under your control I would consider ordering the structure slightly different to aid in future improvements and additions.

hope this gets you going!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900