-
May 13th, 2010, 07:38 AM
#1
Thread Starter
Fanatic Member
xml element altering issues
ok I have some xml that I read from an xml document.. now I need to take that same xml and then change one field and then drop back to a file again..
I'm using this code to read it..
Code:
Private Sub ExtractTriggerData()
PrintTrace("ExtractTriggerData")
Dim theDoc As MSXML2.DOMDocument
Dim FilenamePath As String
Dim PatientLine As String
Dim x As Short
Dim XMLFieldAttempt As String
On Error GoTo BadXMLDoc
FilenamePath = My.Settings.XMLDropDir & My.Settings.XMLDropDirfile
theDoc = New MSXML2.DOMDocument
theDoc.load(FilenamePath) '.load for file .loadxml for POST
x = theDoc.xml.Length
'clear the record first
XMLFieldAttempt = "jobtype"
MyTriggerData.WorkType = My.Settings.JobType
MyTriggerData.MR = ""
MyTriggerData.strAccItn = New String() {"", "", "", "", "", "", ""}
XMLFieldAttempt = "Accession1"
MyTriggerData.strAccItn(0) = theDoc.selectSingleNode("/ChunkData/Accessions/Accession1").text
XMLFieldAttempt = "Note1"
PatientLine = theDoc.selectSingleNode("/ChunkData/Note/Note1").text
MyTriggerData.PtLast = ExtractData(PatientLine, "Patient:", ",")
MyTriggerData.PtFirst = ExtractData(PatientLine, ",", "MR:")
MyTriggerData.MR = ExtractData(PatientLine, "MR:", "Sex:")
'MyTriggerData.MR = theDoc.selectSingleNode("/DictationData/New/PatientID").text
'sometimes the xml will have this layout HOSP000000MRN, just get the MRN
'MyTriggerData.MR = Microsoft.VisualBasic.Right(MyTriggerData.MR, 12)
'MyTriggerData.PatienName = theDoc.selectSingleNode("/DictationData/New/PatientName").text
'MyTriggerData.UserName = theDoc.selectSingleNode("/DictationData/New/UserName").text
'first turn off the file checking timer.. we dont want to keep looking, since we have one
tmrFileCheck.Enabled = False
'but also turn on the timer to see if they finished or not
XMLFieldAttempt = "n/a"
On Error GoTo 0
'txtFileName.Text = My.Application.Info.DirectoryPath & "\test2.xml"
'New code to reformat the XML for hologic May 2010
If My.Settings.HologicDropEnabled = True Then
Call ReformatXML(theDoc, PatientLine)
Call DropHologicXMLFile(theDoc)
End If
Exit Sub
BadXMLDoc:
x = MsgBox("Please check the output of the XML document: " & vbCrLf & "Missing key field - " & XMLFieldAttempt & vbCrLf & "(Error #-" & Str(Err.Number) & ")", MsgBoxStyle.OkOnly, "Bad XML Format, Stopping bridge")
' Call PrintStack()
mnuStop_Click(mnuStop, New System.EventArgs())
End Sub
so what I read about Xpath is that you cant really alter xml with it.. so I then found this code..
Code:
Private Sub ReformatXML(ByVal theDoc As MSXML2.DOMDocument, ByVal PatientLine As String)
Dim NewMR As String
Dim Filenamepath As String
'Dim root As XmlNode = theDoc.DocumentElement
Dim doc As New XmlDocument()
FilenamePath = My.Settings.XMLDropDir & My.Settings.XMLDropDirfile
doc.LoadXml(filenamepath)
Dim root As XmlNode = doc.DocumentElement
'Create a new title element.
Dim elem As XmlElement = doc.CreateElement("/ChunkData/Note/Note1")
NewMR = MyTriggerData.MR
NewMR = NewMR.TrimStart("0"c)
NewMR = NewMR.PadLeft(Val(My.Settings.MinMRLength), "0"c)
Replace(PatientLine, MyTriggerData.MR, NewMR)
elem.InnerText = PatientLine
'Replace the title element.
root.ReplaceChild(elem, root.FirstChild)
End Sub
I tried to pass in the MSXML2.DOMDocument but I get the
system.reflection.targetinvocationexception' occurred in mscorlib.dll
error on the doc.loadxml call
so then as you can see I tried to use a new open of the document from scratch and I get the same error.. I read that its because I may have 2
imports that have the same method ? I had 2 XML imports and made
it only this.. but still get the same error..
Code:
Imports VB = Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Data
Imports system.Data.Odbc
Imports System.Data.OleDb
Imports System.Collections.Generic
Imports System.Diagnostics.Debug
'Imports System.Xml.XPath
Imports System.Xml
so I guess.. anyone got any ideas how to fix my issue or a better way to read and change the one field I need to change ?
thanks in advance..
-
May 13th, 2010, 08:46 AM
#2
Re: xml element altering issues
Which version of Visual Studio are you using? If VS2008 or higher consider something like the following which demos reading, changing, saving information along with testing to see if new information needs to be added.
Code:
Module Module1
Private Doc As New XDocument
Private Const SettingsFile As String = "Settings.xml"
Private ShowMyDialog As Boolean = False
Sub Main()
Doc = XDocument.Load(SettingsFile)
ToggleShowDialog()
If NeedToAdd() Then
AddOn()
End If
Console.WriteLine(Doc.ToString)
Console.ReadLine()
End Sub
Private Sub ToggleShowDialog()
Dim Setting = From O In Doc...<Settings>.<MyDialog>
If Convert.ToBoolean(Setting.@Show) Then
Setting.@Show = "False"
Else
Setting.@Show = "True"
End If
Doc.Save(SettingsFile)
End Sub
Private Function NeedToAdd() As Boolean
Return IsNothing( _
( _
From d In Doc.<Settings> Select d.<AnotherSetting>.Value _
).FirstOrDefault)
End Function
Private Sub AddOn()
Dim Info = Doc.<Settings>(0)
Info.FirstNode.AddAfterSelf(<NewSetting>Some Setting</NewSetting>)
Info.LastNode.AddAfterSelf(<AnotherSetting Name="Foo"></AnotherSetting>)
Doc.Save(SettingsFile)
End Sub
End Module
XML file to be placed in the application debug folder
Code:
<?xml version="1.0" encoding="utf-8"?>
<Settings>
<MyDialog Show="True" />
<Use Default="Development" />
</Settings>
-
May 13th, 2010, 08:47 AM
#3
Re: xml element altering issues
First decide if you want to use the MSXML COM compoent. or the .NET XML namespace. That's where your conflict comes in. Doesn't help that the last code sample mixes the two as well.
True- you can't edit with XPath... but XPath can get you to were you want to be, then once you have the node you want to work with, you can edit it from there.
Oh. and so you know, the LoadXML method is for loading the XML Document with an XML STRING .... if you're loading a file, then you want the .Load(filepath) method...
Code:
Private Sub ReformatXML(ByVal PatientLine As String)
Dim NewMR As String
Dim Filenamepath As String = My.Settings.XMLDropDir & My.Settings.XMLDropDirfile
Dim doc As New XmlDocument()
doc.Load(filenamepath)
Dim root As XmlNode = doc.DocumentElement
'Create a new title element.
Dim elem As XmlElement = doc.CreateElement("/ChunkData/Note/Note1")
NewMR = MyTriggerData.MR
NewMR = NewMR.TrimStart("0"c)
NewMR = NewMR.PadLeft(Val(My.Settings.MinMRLength), "0"c)
Replace(PatientLine, MyTriggerData.MR, NewMR)
elem.InnerText = PatientLine
'Replace the title element.
root.ReplaceChild(elem, root.FirstChild)
End Sub
-tg
-
May 13th, 2010, 08:53 AM
#4
Thread Starter
Fanatic Member
Re: xml element altering issues
learn something new very day.. thanks..
I'll take a look at this and tinker away... problem is the more
I read, the more ideas I try and the more it all
mixes together.. and then ERRORS !!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|