我已经检查并尝试了多个解决方案来解决这个问题,但仍无法解决这个问题。
我从数据集创建XML文件(从Excel文件填充)。该文件工作正常,我已经针对XSD验证了它。我剩下的唯一问题是我在根元素中定义的名称空间前缀没有出现在XMLDocument的任何元素中。以下是生成XMLDocument的代码(注意 - partDS是加载XML文件的数据集):
Private Sub WriteXML_Click(sender As Object, e As EventArgs) Handles Writer.Click
Dim fName As String = "C:\Users\ThisUser\Desktop\Plain.xml"
partDS.DataSetName = "XMLDataDelivery"
partDS.WriteXml(fName)
Dim xDoc As New XmlDocument
Dim schemaLocation As XmlAttribute = Nothing
Dim schemaString As String = "http://www.w3.org/2001/XMLSchema-instance"
Dim schemaValue As String = "http://www.tgin.org/trident/7143706-001 7143706-001RevA.xsd"
Dim namespaceAttribute As XmlAttribute = Nothing
Dim nsString As String = "http://www.w3.org/2000/xmlns/"
Dim nsValue As String = "http://www.tgin.org/trident/7143706-001"
Dim commonTimeElement As XmlElement = Nothing
Dim commonTimeAttribute As XmlAttribute = Nothing
Dim commonTimeString As String = "/XMLDataDelivery/CommonHeader/SubmittedDateTimeUTC"
Dim dataTimeElement As XmlElement = Nothing
Dim dataTimeAttribute As XmlAttribute = Nothing
Dim dataTimeString As String = "/XMLDataDelivery/Data/LogisticsEvent/DateTime"
Dim timeAttributeString = "dl:DateTime12HourWithoutFeb29"
Dim dateTimeNodeList As XmlNodeList = Nothing
Dim parentLevels As XmlNodeList = xDoc.SelectNodes("/XMLDataDelivery/Data/LogisticsEvent")
Dim higherLevels As XmlNodeList = xDoc.SelectNodes("/XMLDataDelivery/Data/LogisticsEvent/Activity")
Dim partLevels As XmlNodeList = xDoc.SelectNodes("/XMLDataDelivery/Data/LogisticsEvent/AssemblyItem")
xDoc.Load(fName)
'define XML namespace and attributes in XML header
namespaceAttribute = xDoc.CreateAttribute("xmlns", "dl", nsString)
namespaceAttribute.Value = nsValue
xDoc.DocumentElement.Attributes.Append(namespaceAttribute)
schemaLocation = xDoc.CreateAttribute("xsi", "schemaLocation", schemaString)
schemaLocation.Value = schemaValue
xDoc.DocumentElement.SetAttributeNode(schemaLocation)
'define and set attributes for the dateTime elements in the XML
commonTimeElement = xDoc.SelectSingleNode(commonTimeString)
commonTimeAttribute = xDoc.CreateAttribute("xsi", "type", schemaString)
commonTimeAttribute.Value = timeAttributeString
commonTimeElement.SetAttributeNode(commonTimeAttribute)
dataTimeAttribute = xDoc.CreateAttribute("xsi", "type", schemaString)
dataTimeAttribute.Value = timeAttributeString
Dim i As Integer = 0
Do Until i > parentLevels.Count - 1
parentLevels.ItemOf(i).InsertBefore(partLevels.ItemOf(i), higherLevels.ItemOf(i))
i += 1
Loop
dateTimeNodeList = xDoc.SelectNodes("/XMLDataDelivery/Data/LogisticsEvent/DateTime")
For Each item As XmlElement In dateTimeNodeList
item.SetAttribute("type", schemaString, timeAttributeString)
Next
xDoc.Save(fName)
End Sub
生成与以下类似的XML:
<XMLDataDelivery xmlns:dl="http://www.tgin.org/trident/7143706-001" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.tgin.org/trident/7143706-001 7143706-001RevA.xsd">
<Data>
<Location>Site01</Location>
<LogisticsEvent>
<DateTime>01-JUL-2015 12:00:00 PM</DateTime>
<AssemblyItem>
<ProductName>Test Part1</ProductName>
<ProductNumber>998741</ProductNumber>
<ProductDash>050</ProductDash>
<SerialNumber>LX2247</SerialNumber>
<ProductRevision>R</ProductRevision>
</AssemblyItem>
<Activity>Stat Change</Activity>
<Condition>Good</Condition>
<Usage>Production</Usage>
<LogisticsEvent>
</Data>
</XMLDataDelivery>
现在从我的研究中记得(在vb.net中编程XML仍然是新的),如果命名空间是在根中定义的,那么同一命名空间的前缀不需要(或不会?)出现在XMLDocument中的每个节点上。
虽然我同意这一点(并且我的验证员同意这是一个有效的XML文件),但如果前缀未附加到每个节点,则上传这些文件的系统将无法通过验证。
我尝试过多次修复,只需定义根前缀(xDoc.DocumentElement.prefix =&#34; dl&#34;),尝试不同的属性设置,使用namespacemanager,但仍然没有任何东西可以坚持下去。
XML文件是从头开始创建的(不是从任何模板加载的)。我确实尝试了命令xDoc.GetNamespaceOfPrefix(&#34; dl&#34;)但是没有返回任何内容。
对不起,这很长,但只是想确保我有足够的信息。感谢任何对此进行评论的人。
答案 0 :(得分:1)
没有意识到Sam Ax的意思(如果可能的话,会提出你的评论),直到我进一步深入研究。最后,我没有意识到我可以更改数据集中数据列的前缀以与根中声明的命名空间相关联:
partDS.Tables("CommonHeader").Prefix = "dl"
partDS.Tables("DataHeader").Prefix = "dl"
partDS.Tables("Data").Prefix = "dl"
partDS.Tables("LogisticsEvent").Prefix = "dl"
partDS.Tables("AssemblyItem").Prefix = "dl"
Dim logCol As Integer = 0
Dim assyCol As Integer = 0
Do Until logCol > partDS.Tables("LogisticsEvent").Columns.Count - 1
partDS.Tables("LogisticsEvent").Columns(logCol).Prefix = "dl"
logCol += 1
Loop
Do Until assyCol > partDS.Tables("AssemblyItem").Columns.Count - 1
partDS.Tables("AssemblyItem").Columns(assyCol).Prefix = "dl"
assyCol += 1
Loop
partDS.DataSetName = "XMLDataDelivery"
partDS.Namespace = "http://www.tgin.org/trident/7143706-001"
partDS.Prefix = "dl"
非常感谢你的帮助。