我如何编写如下的Xml [英] How Do I Write Such Xml As Below

查看:50
本文介绍了我如何编写如下的Xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我使用vb.net编写xml文件有点新鲜,我想编写下面附加的xml文件,但经过多次尝试,我无法写这个xml,请。帮助我



问候!

Raj



xml formate就像下面想写怎么样?



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 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>





编辑:




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")

推荐答案

在这里尝试这个代码,它写的第一部分很好,它有点麻烦,但你可以看到vb如何与xml,xmlwriter一起工作可能是一个更好的解决方案,但这应该让你去。





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.


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 ,我将在下面发布这个改进的解决方案








ok, I will post this as an improved solution below


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


这篇关于我如何编写如下的Xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆