如果直接写
- Dim xd As New XmlDocument()
- xd.Load(xmlPath)
- Dim root As XmlNode = xd.DocumentElement
- Dim xn As XmlNode = xd.CreateElement("ItemType")
- Dim nameAttr As XmlAttribute = xd.CreateAttribute("Name")
- nameAttr.Value = name
- xn.Attributes.Append(nameAttr)
- Dim displayNameAttr As XmlAttribute = xd.CreateAttribute("DisplayName")
- displayNameAttr.Value = displayName
- xn.Attributes.Append(displayNameAttr)
- root.AppendChild(xn)
- xd.Save(xmlPath)
产出的xml会有一个空的xmlns属性, e.g:
<FileExtension Name=".cd" ContentType="ClassDiagram" xmlns=""/>
解决这个问题,只需要在CreateElement的时候改为:
Dim xn As XmlNode = xd.CreateElement("ItemType", root.NamespaceURI)
就可以了