Dim xmlData As System.Xml.Linq.XDocument =
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Body>
<m:getInHospType xmlns:m="http://webservice.clini.org/">
<arg0>
<areaId>aaa</areaId>
<inType>张三</inType>
<remark1>1</remark1>
<remark2/>
<remark3/>
<remark4>
</remark4>
</arg0>
<arg1>
<areaId>bbb</areaId>
<inType>sf</inType>
<remark1>2</remark1>
<remark2/>
<abc>aa</abc>
<remark3/>
<remark4>
</remark4>
</arg1>
</m:getInHospType>
</Body>
</SOAP-ENV:Envelope>
一、增加元素
Add_XML_Element和Add_XML_Element_Ex
Add_XML_Element增加元素,父节点相同的情况下,找到的第一个父节点下增加单个节点
Add_XML_Element_Ex父节点相同的情况下,根据索引值得到的父节点下增加多个节点
Dim string_reader As New System.IO.StringReader(xmlData.Declaration.ToString & xmlData.ToString)
Dim m_xmldll As New xmldll(string_reader)
Dim nt As New Xml.NameTable
Dim nsmgr As New Xml.XmlNamespaceManager(nt)
nsmgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
nsmgr.AddNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/")
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
nsmgr.AddNamespace("m", "http://webservice.clini.org/")
m_xmldll.Add_XML_Element("Body", "SOAP-ENV:Envelope", "", "", "", nsmgr)
'm_xmldll.Add_XML_Element("test", "SOAP-ENV:Envelope", "tvalue", "m", "http://www.w3.org/2001/xml", nsmgr)
m_xmldll.Save("z:\test.xml")
Dim m_xmldll1 As New xmldll("z:\test.xml")
Dim element(2) As xmldll.xml_element
element(0).name = "d1"
element(0).value = "d100"
element(1).name = "e1"
element(1).value = "e100"
element(2).name = "f1"
element(2).value = "f100"
m_xmldll1.Add_XML_Element_Ex(element, "Body", 1, "", "", nsmgr)
m_xmldll.Save("z:\test1.xml")
二、增加属性值
Add_XML_Attribute和Add_XML_Attribute_Ex
Add_XML_Attribute增加元素属性值,在多个相同父节点下,找到的第一个父节点增加元素属性值
Add_XML_Attribute_Ex多个相同父节点下,根据索引得到的父节点下增加元素属性值
Dim string_reader As New System.IO.StringReader(xmlData.Declaration.ToString & xmlData.ToString)
Dim m_xmldll As New xmldll(string_reader)
Dim nt As New Xml.NameTable
Dim nsmgr As New Xml.XmlNamespaceManager(nt)
nsmgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
nsmgr.AddNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/")
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
nsmgr.AddNamespace("m", "http://webservice.clini.org/")
Dim m_attribute(0) As Xml.XmlAttribute
m_attribute(0) = (New System.Xml.XmlDocument).CreateAttribute("SOAP-ENC")
m_attribute(0).Value = "http://schemas.xmlsoap.org/soap/encodings/"
Dim rtn As Boolean = m_xmldll.Add_XML_Attribute("arg0", m_attribute, "m:getInHospType", nsmgr)
m_xmldll.Add_XML_Element("arg1", "m:getInHospType", "", "", "", nsmgr)
Dim element(1) As xmldll.xml_element
element(0).name = "areaId"
element(0).value = "a1"
element(1).name = "inType"
element(1).value = "赵五"
m_xmldll.Add_XML_Element_Ex(element, "arg1", 1, "", "", nsmgr)
m_xmldll.Save("z:\test.xml")
Dim m_attribute1(0) As Xml.XmlAttribute
m_attribute1(0) = (New System.Xml.XmlDocument).CreateAttribute("SOAP-ENC")
m_attribute1(0).Value = "http://schemas.xmlsoap.org/soap/encodings/11"
Dim m_attribute2(0) As Xml.XmlAttribute
m_attribute2(0) = (New System.Xml.XmlDocument).CreateAttribute("SOAP-ENC")
m_attribute2(0).Value = "http://schemas.xmlsoap.org/soap/encodings/12"
Dim m_xmldll1 As New xmldll("z:\test.xml")
m_xmldll1.Add_XML_Attribute_Ex("areaId", m_attribute1, "arg1", 2)
m_xmldll1.Add_XML_Attribute_Ex("inType", m_attribute2)
m_xmldll1.Save("z:\test1.xml")
三、删除节点
Remove_XMLnode
Remove_XMLnode_Ex
在以上XML代码中加入以下内容,保存为test1.xml,
<bbb>
<f:arg1 xmlns:f="http://schemas.xmlsoap.org/soap/encodings/12">
<areaId>bbb</areaId>
<inType>sf</inType>
<remark1>2</remark1>
<remark2 />
<abc>aa</abc>
<remark3 />
<remark4>
</remark4>
</f:arg1>
</bbb>
删除新加的arg1
Dim m_xmldll As New xmldll("z:\test1.xml")
Dim nt As New Xml.NameTable
Dim nsmgr As New Xml.XmlNamespaceManager(nt)
nsmgr.AddNamespace("f", "http://schemas.xmlsoap.org/soap/encodings/12")
m_xmldll.Remove_XMLnode_Ex("f:arg1", "bbb", 0,nsmgr)
m_xmldll.Save("z:\test2.xml")
四、删除节点属性
Remove_XMLnode_Attribute
Remove_XMLnode_Attribute_Ex 参数attribute_name如为“”,则全部删除
上述XML代码中加入,保存为test1.xml
<bbb><f:arg1 xmlns:f="http://schemas.xmlsoap.org/soap/encodings/12" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsd1="http://www.w3.org/2001/XMLSchema11">
<areaId>bbb</areaId>
<inType>sf</inType>
<remark1>2</remark1>
<remark2 />
<abc>aa</abc>
<remark3 />
<remark4>
</remark4>
</f:arg1></bbb>
注:带前缀的命名空间不可删除。如本例中xmlns:f部分。
Dim m_xmldll As New xmldll("z:\test1.xml")
Dim nt As New Xml.NameTable
Dim nsmgr As New Xml.XmlNamespaceManager(nt)
nsmgr.AddNamespace("f", "http://schemas.xmlsoap.org/soap/encodings/12")
Dim rtn As Boolean = m_xmldll.Remove_XMLnode_Attribute_Ex("f:arg1", "bbb", 0, "", nsmgr)
If rtn = True Then
m_xmldll.Save("z:\test2.xml")
End If
五、获取节点值
Get_XMLnode_Value
Get_XMLnode_Value_Ex
Dim m_xmldll As New xmldll("z:\test1.xml")
Dim nt As New Xml.NameTable
Dim rtn As String = m_xmldll.Get_XMLnode_Value_Ex("inType", "arg1", 1)
六、设置节点值
Set_XMLnode_Value
Set_XMLnode_Value_Ex
Dim m_xmldll As New xmldll("z:\test1.xml")
Dim nt As New Xml.NameTable
Dim rtn As String = m_xmldll.Set_XMLnode_Value_Ex("inType", "FFFF", "arg1", 1)
If rtn = True Then
m_xmldll.Save("z:\test2.xml")
End If
七、获取节点属性值
Get_XMLnode_Attrib_Value
Get_XMLnode_Attrib_Value_Ex
Dim m_xmldll As New xmldll("z:\test1.xml")
Dim nt As New Xml.NameTable
Dim nsmgr As New Xml.XmlNamespaceManager(nt)
nsmgr.AddNamespace("f", "http://schemas.xmlsoap.org/soap/encodings/12")
nsmgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
'Dim rtn As String = m_xmldll.Get_XMLnode_Attrib_Value("SOAP-ENV:Envelope", "xmlns:SOAP-ENC", , nsmgr)
Dim rtn As String = m_xmldll.Get_XMLnode_Attrib_Value_Ex("f:arg1", "xmlns:xsd", "bbb", 1, nsmgr)
八、设置节点属性值
Set_XMLnode_Attrib_Value
Set_XMLnode_Attrib_Value_Ex
Dim nt As New Xml.NameTable
Dim nsmgr As New Xml.XmlNamespaceManager(nt)
nsmgr.AddNamespace("f", "http://schemas.xmlsoap.org/soap/encodings/12")
nsmgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
'Dim rtn As String = m_xmldll.Set_XMLnode_Attrib_Value("f:arg1", "xmlns:xsd", "http://www.baidu.com", "bbb", nsmgr)
Dim rtn As String = m_xmldll.Set_XMLnode_Attrib_Value_Ex("f:arg1", "xmlns:xsd", "http://www.baidu.com11", "bbb", 1, nsmgr)
If rtn = True Then
m_xmldll.Save("z:\test2.xml")
End If
九、xml转datatable
XMLtoTable
XMLtoTablesingle单条记录转datatable
这里用网上采集的一段xml代码
<?xml version="1.0" encoding="utf-8"?>
<root>
<Record>
<id>
0</id>
<name>穿甲弹</name>
<type>0</type>
<iconUrl>resource\bullet\icon\子弹1.png</iconUrl>
<modelUrl>resource\bullet\model\子弹1.swf</modelUrl>
<effectUrl>resource\effect\子弹1特效.swf</effectUrl>
<attackArea>1</attackArea>
<cdMaxTime>5000</cdMaxTime>
<attack>20</attack>
<speed>10</speed>
<slowSpeed>0</slowSpeed>
<slowTime>0</slowTime>
<attackCD>500</attackCD>
<num>5</num>
</Record>
<Record>
<id>
1</id>
<name>爆破弹</name>
<type>1</type>
<iconUrl>resource\bullet\icon\子弹2.png</iconUrl>
<modelUrl>resource\bullet\model\子弹1.swf</modelUrl>
<effectUrl>resource\effect\子弹1特效.swf</effectUrl>
<attackArea>20</attackArea>
<cdMaxTime>5000</cdMaxTime>
<attack>5</attack>
<speed>10</speed>
<slowSpeed>0</slowSpeed>
<slowTime>0</slowTime>
<attackCD>500</attackCD>
<num>5</num>
</Record>
<Record>
<id>
2</id>
<name>普通弹</name>
<type>2</type>
<iconUrl>resource\bullet\icon\子弹3.png</iconUrl>
<modelUrl>resource\bullet\model\子弹1.swf</modelUrl>
<effectUrl>resource\effect\子弹1特效.swf</effectUrl>
<attackArea>1</attackArea>
<cdMaxTime>5000</cdMaxTime>
<attack>10</attack>
<speed>10</speed>
<slowSpeed>0</slowSpeed>
<slowTime>0</slowTime>
<attackCD>500</attackCD>
<num>5</num>
</Record>
<Record>
<id>
3</id>
<name>急速弹</name>
<type>3</type>
<iconUrl>resource\bullet\icon\子弹1.png</iconUrl>
<modelUrl>resource\bullet\model\子弹1.swf</modelUrl>
<effectUrl>resource\effect\子弹1特效.swf</effectUrl>
<attackArea>1</attackArea>
<cdMaxTime>5000</cdMaxTime>
<attack>8</attack>
<speed>10</speed>
<slowSpeed>0</slowSpeed>
<slowTime>0</slowTime>
<attackCD>100</attackCD>
<num>5</num>
</Record>
<Record>
<id>
4</id>
<name>粘性弹</name>
<type>4</type>
<iconUrl>resource\bullet\icon\子弹2.png</iconUrl>
<modelUrl>resource\bullet\model\子弹1.swf</modelUrl>
<effectUrl>resource\effect\子弹1特效.swf</effectUrl>
<attackArea>1</attackArea>
<cdMaxTime>5000</cdMaxTime>
<attack>12</attack>
<speed>10</speed>
<slowSpeed>5</slowSpeed>
<slowTime>2000</slowTime>
<attackCD>500</attackCD>
<num>5</num>
</Record>
</root>
代码如下:
Dim m_xmldll As New xmldll("z:\z1.xml")
DataGridView1.DataSource = m_xmldll.XMLtoTable("Record")
'DataGridView1.DataSource = m_xmldll.XMLtoTableSingle("Record", "", 1)
效果如下:
十、xml导出到treeview
xmlToTreeview
xmlToTreeviewsingle
这里用开头的XML代码,修改的部分
<arg1>
<areaId>bbb</areaId>
<inType>sf</inType>
<remark1>2</remark1>
<remark2 />
<abc>aa</abc>
<remark3 />
<remark4>
</remark4>
</arg1>
<arg1>
<areaId SOAP-ENC="http://schemas.xmlsoap.org/soap/encodings/11">a1</areaId>
<inType>赵五</inType>
<remark4>rk4
</remark4>
</arg1>
执行的代码:
m_xmldll.xmlToTreeview("arg1", TreeView1, "m:getInHospType", Nothing)
'm_xmldll.xmlToTreeviewSingle("arg1", TreeView1, "m:getInHospType", 0, Nothing, ImageList1)
效果如下 :
在form中加入一个imagelist控件,加入4个图标,分别对应项目和值的点击和未点击状态
执行代码
m_xmldll.xmlToTreeview("arg1", TreeView1, "m:getInHospType", Nothing, ImageList1)
十一、xml转TXT文件、TXT转XML
XMLtoTXT
使用九中的XML代码
执行代码:
m_xmldll.XMLtoTXT("arg1", "z:\111.txt", System.Text.Encoding.GetEncoding("GB2312"), "")
结果如下:
TXTtoXML
Dim m_xml As New xmldll1
m_xml.TXTtoXML("z:\111.txt", System.Text.Encoding.GetEncoding("GB2312"), "z:\t111.xml", System.Text.Encoding.UTF8, "root", "record")
十二、杂项
XMLFile_Format
CreateXmlFile
GetNodeCount
Dim m_xmldll As New xmldll("z:\test1.xml")
Dim rtn As integer= m_xmldll.GetNodeCount("f:arg1", "")
创建XML文件默认编码UTF-8
Dim m_xml As New xmldll1
m_xml.CreateXmlFile("z:\111.xml")
资源下载
http://download.csdn.net/detail/iceou/9431673