Remove NewDataSet node error (Illegal char in path)

darkstrike20

Member
Joined
Jul 17, 2014
Messages
7
Programming Experience
Beginner
Hi i'm trying to remove the NewDataSet node from an exsiting xml file but i receive an error during my runtime - Illegal char in path

Example of my xml document :-
<?xml version="1.0" standalone="yes"?><NewDataSet>
<trs_bills>
<ID>145</ID>
<TrsOutlet>V001</TrsOutlet>
<TrsDate>2014-07-03T00:00:00+08:00</TrsDate>
<DateCreated>2014-07-03T17:16:05+08:00</DateCreated>
<TerminalCode>001</TerminalCode>
<TrsNo>V001#001#20140703171605#CSH#1000169</TrsNo>
<BillNo>1000169</BillNo>
<ReceiptNo>001-1000101</ReceiptNo>
<StaffCode>666</StaffCode>
<StaffName />
<AccountCode>NON</AccountCode>
<AccountName>Chan</AccountName>
<AgentCode>666</AgentCode>
<AgentName />
<TrsTime>1899-12-30T17:17:00+08:00</TrsTime>
<SubTotal>43.0000</SubTotal>
<ItemDiscAmt>0.0000</ItemDiscAmt>
<Tax1Amt>2.5800</Tax1Amt>
<Tax2Amt>4.3000</Tax2Amt>
<GrossAmt>43.0000</GrossAmt>
<BillDiscAmt>0.0000</BillDiscAmt>
<NetAmt>49.8800</NetAmt>
<RoundedDiff>0.02</RoundedDiff>
<RoundedAmt>49.90</RoundedAmt>
<ValueDiscAmt>0.00</ValueDiscAmt>
<EndTotalAmt>49.90</EndTotalAmt>
<PaidStatus>1</PaidStatus>
</NewDataSet>




Dim doc2 As XmlDocument = New XmlDocument()


Dim xtr As XmlTextReader = New XmlTextReader("D:\XMLfiles\" + KMsg + ".xml")


doc2.Load(xtr)
xtr.Close()


'Get the <NewDataSet> node
Dim node As XmlNode = doc2.GetElementsByTagName("NewDataSet")(0)



Dim nodelistParent As XmlNode = node.ParentNode



Dim tmpnodelist As XmlNodeList = node.ChildNodes



nodelistParent.RemoveChild(node)



Dim newdoc As XmlDocument = New XmlDocument()


'Error here - Illegal characters in path.
newdoc.Load(doc2.InnerXml)


'Append all the child nodes of <NewDataSet> to its parentnode
While tmpnodelist.Count > 0


nodelistParent.AppendChild(tmpnodelist(0))


End While


'Insert the remaining portion into newdoc
Dim newnode As XmlNode = newdoc.GetElementsByTagName(nodelistParent.Name)(0)
newnode.InnerXml = nodelistParent.InnerXml


'Save document
newdoc.Save("D:\" + KMsg + ".xml")
 
Hi and welcome to the forum,

Forgetting the XML information that you have shown for the moment, the error "Illegal char in path" implies that the Path to the XML filename is incorrect but you have not detailed exactly where you get that error so that's a bit of a guess and there is no indication what "KMsg" means or relates to.

If you provide the exact error message that is shown and where it is shown we may be able to help you better?

Cheers,

Ian
 
Here , this part of my code had an error. I don't really understand on the .innerXml though

'Error here - Illegal characters in path.

newdoc.Load(doc2.InnerXml)
 
Hi,

I have had chance to have a better look at this now and there are a few things to note:-

1) The first issue you have got is that the xml snippet you posted is an incorrectly formatted XML structure since it is missing its closing tag for the node "trs_bills"

2) I can see what you are trying to do but you are missing a few bits. After you have called:-

Dim node As XmlNode = doc2.GetElementsByTagName("NewDataSet")(0)


You will then find that node.InnerXML property will contain all the xml nodes within " NewDataSet" excluding the "NewDataSet" node.

3) The next thing to learn is that if you want to create a new document from an XML String within your application then you need to call the LoadXml method on the new document since that expects a string to create an xml file from. In your case you are calling the Load Method with doc2.InnerXml which is supposed to be a filename, a stream or a reader.

4) So, knowing points 2 and 3 above all you should be doing to create the new xml file is:-

newdoc.LoadXml(node.InnerXml)
'Save the new document


5) Please use code tags when posting in the future since it helps to keep the code formatting making things a lot easier to read.

Hope that helps.

Cheers,

Ian
 
Last edited:
Back
Top