帮助保存到XML文件 [英] Help saving to XML file
问题描述
大家好,
我正在尝试将数据存储到XML文件,但无法获取它以所需的方式存储.我确信问题很明显并且很容易解决,但是我对XML没有太多的经验,这使我感到困惑.基本上,我使用下面的例程(位于底部)从隔离的存储中打开现有文件,更改与感兴趣的标签相对应的值,然后保存文件. XML开始像:
Hi All,
I am trying to store data to an XML file but I cannot get it to store the way I want. I am sure the problem is something very obvious and easy to fix but I dont have much experience with XML so it is confusing me. Basically I am using the routine below (at bottom) to open an existing file from within isolated storage, change a value corresponding to a tag of interest and save the file. The XML starts off like:
<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
<WorkingMXD />
<Test1>Value 1</Test1>
</UserSettings>
并且应该像
那样结束
and should end up like
<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
<WorkingMXD />
<Test1>Value 2</Test1>
</UserSettings>
但最终却像
but instead ends up like
<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
<WorkingMXD />
<Test1>Value 1</Test1>
</UserSettings><?xml version="1.0" encoding="utf-8"?>
<UserSettings>
<WorkingMXD />
<Test1>Value 2</Test1>
</UserSettings>
我用来保存的代码是
The code I use to save is
Public Sub EditSettingsFile(ByVal sTag As String, ByVal sValue As String)
Dim stmWriter As New IsolatedStorageFileStream("PlanningGDBToolsSettings.xml", FileMode.Open, m_pIsoStorage)
Dim writer As New Xml.XmlTextWriter(stmWriter, Encoding.UTF8)
Dim xmldoc As New Xml.XmlDataDocument()
Dim xmlnode As Xml.XmlNodeList
Dim i As Integer
Try
xmldoc.Load(stmWriter)
xmlnode = xmldoc.GetElementsByTagName(sTag)
For i = 0 To xmlnode.Count - 1
If xmlnode(i).Name.Trim = sTag.Trim Then
xmlnode(i).InnerText = sValue.Trim
End If
Next
writer.Formatting = Xml.Formatting.Indented
xmldoc.WriteContentTo(writer)
Catch ex As Exception
MsgBox("OpenSettingsFile - " & ex.Message)
Finally
writer.Flush()
writer.Close()
stmWriter.Close()
End Try
End Sub
谁能让我知道我要去哪里错了.
在此先感谢
Can anyone please let me know where I am going wrong.
Thanks in advance
推荐答案
以各种方式:-)使用XPath选择元素,不要遍历所有元素.我看不到这段代码如何创建您向我们展示的文档,它甚至都不是XML,因此它不适合XmlDataDocument.我认为WriteContentTo在当前文档之后编写文档.使用XmlDocument以及Load和Save方法.
In every way :-) Use XPath to select elements, don''t iterate over all of them. I don''t see how this code could create the document you''re showing us though, it''s not even XML, so it would not fit in an XmlDataDocument. I think WriteContentTo is writing the document AFTER the current one. Use XmlDocument and the Load and Save methods.
该死,我怀疑太多了!这是我用来编辑的代码,下面是我用来创建它的代码.基本上,我检查文件是否存在,如果不存在,则运行创建然后编辑,如果存在,则仅运行前一个编辑子.感谢您的答复,我将研究XPath,让您知道我的发展:-)
Damn I suspected as much! This is code I was using to edit, the code I used to create it is below. Basically I check to see if the file exists and if not I run the create then edit, if it does then I run just the previous edit sub. Thanks for the reply, I will look into XPath and let you know how I go :-)
Public Sub CreateSettingsFile()
Dim stmWriter As New IsolatedStorageFileStream("PlanningGDBToolsSettings.xml", FileMode.Create, m_pIsoStorage)
Dim writer As New Xml.XmlTextWriter(stmWriter, Encoding.UTF8)
Try
writer.Formatting = Xml.Formatting.Indented
writer.WriteStartDocument()
writer.WriteStartElement("UserSettings")
writer.WriteStartElement("WorkingMXD")
writer.WriteString("Some mxd")
writer.WriteEndElement()
writer.WriteStartElement("Test1")
writer.WriteString("Test unsuccessful")
writer.WriteEndElement()
writer.WriteEndElement()
Catch ex As Exception
MsgBox("CreateSettingsFile - " & ex.Message)
Finally
writer.Flush()
writer.Close()
stmWriter.Close()
End Try
End Sub
这篇关于帮助保存到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!