帮助将数据从xml写入文本文件 [英] Help writing data from xml to text file

查看:116
本文介绍了帮助将数据从xml写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究此功能,该功能将读取特定条目的xml文件,如果存在,它将把其信息写入文本文件.从长远来看,我将把名称更改为计算机名称和将添加到主机文件中的IP地址,但是要知道不要使用电子邮件和用户名公开任何网络信息.我可以读取XML文件,但是当它写入文本文件时,其输出为:

Im working on this function that will read a xml file for a specific entry and if its there it will write its information to a text file. in the long run I m will be changing the names to computer names and ip address that will get added to the host file, but for know to not disclose any network information im using email and user names. I can get it to read the xml file but when it writes to a text file its output is :

System.Xml.XmlElement
System.Xml.XmlElement



如果有人可以查看我的代码并告诉我我做错了,那将是很好的.以下是我的XML文件,名为Contact.xml.



if someone can look at my code and tell me what i''m doing wrong that would be great. below is my xml file called Contact.xml.

<?xml version="1.0" encoding="utf-8" ?>
 <CAE>
         <base>
                 <caedata>John</caedata>
         <caepolicy>Smith</caepolicy>
         <basecode>john.smith@xyzmail.com</basecode>
         </base>
         <base>
                 <caedata>Suzan</caedata>
                 <caepolicy>Carter</caepolicy>
                 <basecode>suzan.carter@xyzmail.com</basecode>
         </base>
         <base>
                 <caedata>Tom</caedata>
                 <caepolicy>Baker</caepolicy>
                 <basecode>tom.baker@xyzmail.com</basecode>
         </base>
 </CAE>



这是我创建的函数,单击该按钮会将我放入文本框中的信息提取出来.



here is my function I created and the button click that I have that pulls the information I put into the textbox.

Public Class Form1

    Dim xmlDoc As New XmlDocument()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If redirect(TextBox1.Text) = True Then
            MsgBox("completed")
        Else
            MsgBox("Failed to complete")
        End If

    End Sub

    Function redirect(ByVal BASE As String)
        Dim fileLoc As String = "C:\Users\Zack\Desktop\hostfile.txt"
        Dim CP
        Dim CD
        xmlDoc.Load("C:\Users\Zack\Desktop\Contact.xml")
        Dim xmlContactNodeList As XmlNodeList = xmlDoc.GetElementsByTagName("base")

        For i = 0 To xmlContactNodeList.Count - 1
            If xmlContactNodeList.Item(i).Item("basecode").InnerText = BASE Then
                CP = xmlContactNodeList.Item(i).Item("caepolicy").ToString
                CD = xmlContactNodeList.Item(i).Item("caedata").ToString
                If File.Exists(fileLoc) Then
                    Using w As New System.IO.StreamWriter(fileLoc, True)
                        w.WriteLine(CD & vbCrLf & CP)
                    End Using
                    Return True
                    Exit Function
                End If
            End If
        Next

        Return False
    End Function
End Class

推荐答案

我弄清楚我做错了什么.当我应该一直使用innertext时使用.tostring声明cp和cd im时.更改后,一切正常.
I figured out what I did wrong. when I m declaring cp and cd im using .tostring when I should have been using innertext. once I changed it everything worked.


这篇关于帮助将数据从xml写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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