noel58
asked on
Manipulating an XML file using VB.Net
I've got a .XML file (content of the file at the bottom of the question) and I need to edit certain aspects of it. I can edit specific elements such as SenderID using
doc.GetElementsByTagName(" SenderID") (0).InnerX ml
but I'm struggling to find example code that would allow me to easily change the innertext of both the <Key Type="STORN">As advised by SDS Team</Key> elements. i.e. I need to change "As advised by SDS Team" to something else.
Finally after completing the above changes I need to be able to edit all the innertext of most of the nodes following <SDLT> with ease...so examples of how to accomplish this would also be appreciated.
Hope this is clear, thanks in advance for any help
Simon
<?xml version="1.0" encoding="UTF-8"?>
<!-- PLEASE NOTE - This is an example to show the layout of a valid XML
test message for an SDLT submission. V1.0 10 Jan 05 -->
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</Enve lopeVersio n>
<Header>
<MessageDetails>
<Class>IR-SDLT-LTR</Class>
<Qualifier>request</Qualif ier>
<Function>submit</Function >
<CorrelationID />
<Transformation>XML</Trans formation>
<GatewayTest>1</GatewayTes t>
<GatewayTimestamp />
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>MSD2058</SenderI D>
<Authentication>
<Method>clear</Method>
<Role>principal</Role>
<Value>testing1</Value>
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys Type="1000002058">
<Key Type="STORN">As advised by SDS Team</Key>
</Keys>
<TargetDetails>
<Organisation>IR</Organisa tion>
</TargetDetails>
<ChannelRouting>
<Channel>
<URI>2058</URI>
<Product>Themis</Product>
<Version>0.0.1</Version>
</Channel>
</ChannelRouting>
</GovTalkDetails>
<Body>
<IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/SDLT">
<IRheader>
<TestMessage>0</TestMessag e>
<Keys>
<Key Type="STORN">As advised by SDS Team</Key>
</Keys>
<PeriodEnd>2004-03-01</Per iodEnd>
<DefaultCurrency>GBP</Defa ultCurrenc y>
<IRmark Type="generic">Include a valid IRmark here</IRmark>
<Sender>Individual</Sender >
</IRheader>
<SDLT>
<UTRN>153492288MT</UTRN>
<TransactionDetails>
<TransactionDescription>F< /Transacti onDescript ion>
<EffectiveDate>2004-02-01< /Effective Date>
<RestrictionsAffecting Apply="no">
</RestrictionsAffecting>
<ContractDate>2004-02-01</ ContractDa te>
<LandExchanged Exchanged="no">
</LandExchanged>
<PursuantToOption>no</Purs uantToOpti on>
<TaxCalculation>
<ClaimingRelief Claiming="no">
</ClaimingRelief>
<Consideration>
<TotalConsideration>100000 .00</Total Considerat ion>
<FormCode>30</FormCode>
</Consideration>
<LinkedTransaction IsLinked="no">
</LinkedTransaction>
<TotalDue>1000.00</TotalDu e>
<AmountPaid IncludesPenalties="no">100 0.00</Amou ntPaid>
</TaxCalculation>
</TransactionDetails>
<LandDetail>
<NumberOfProperties>1</Num berOfPrope rties>
<Property>
<PropertyType>01</Property Type>
<AddressOfLand AddressExtended="no">
<PostCode>AA1 1AA</PostCode>
<HouseNumber>100</HouseNum ber>
<Line>The Road</Line>
<Line>The Town</Line>
</AddressOfLand>
<LAnumber>9051</LAnumber>
<TitleNumber>AA11111</Titl eNumber>
<NLPGUPRN>111111222222</NL PGUPRN>
<PlanSubmitted>no</PlanSub mitted>
<InterestTransfered>FP</In terestTran sfered>
</Property>
</LandDetail>
<VendorDetails>
<NumberOfVendors>1</Number OfVendors>
<Vendor>
<Name>
<Title>Mr</Title>
<Forename>Tax</Forename>
<CompanyOrSurname>Payer</C ompanyOrSu rname>
</Name>
<Address>
<PostCode>AA1 1AA</PostCode>
<HouseNumber>100</HouseNum ber>
<Line>The Road</Line>
<Line>The Town</Line>
</Address>
</Vendor>
<AgentDetails>
<Name>Solicitor & Co</Name>
<Address>
<PostCode>BB1 1BB</PostCode>
<Line>The Office</Line>
<Line>The Road</Line>
<Line>The Town</Line>
</Address>
<DXnumber>11111 Town</DXnumber>
<EmailAddress>a@a.co.uk</E mailAddres s>
<Reference>A1111/AA/AA</Re ference>
<Telephone>01111 11111</Telephone>
</AgentDetails>
</VendorDetails>
<PurchaserDetails>
<NumberOfPurchasers>1</Num berOfPurch asers>
<Purchaser>
<Name>
<Title>Miss</Title>
<Forename>Tax</Forename>
<CompanyOrSurname>Payer</C ompanyOrSu rname>
</Name>
<SameAddressAsLand>yes</Sa meAddressA sLand>
<NINO>AA111111A</NINO>
<Trustee>no</Trustee>
<VendorConnected>no</Vendo rConnected >
<CertificateAddress>Agent< /Certifica teAddress>
<AuthoriseAgent>yes</Autho riseAgent>
<AgentDetails>
<Name>A Conveyancer</Name>
<Address>
<PostCode>AA1 2AB</PostCode>
<HouseNumber>1</HouseNumbe r>
<Line>A Road</Line>
<Line>A Town</Line>
</Address>
<DXnumber>22222 Town</DXnumber>
<Reference>A111/BB</Refere nce>
<Telephone>02222 222222</Telephone>
</AgentDetails>
</Purchaser>
</PurchaserDetails>
<SupplementarySections>
<SDLT2Count>0</SDLT2Count>
<SDLT3Count>0</SDLT3Count>
<SDLT4Count>0</SDLT4Count>
</SupplementarySections>
</SDLT>
</IRenvelope>
</Body>
</GovTalkMessage>
doc.GetElementsByTagName("
but I'm struggling to find example code that would allow me to easily change the innertext of both the <Key Type="STORN">As advised by SDS Team</Key> elements. i.e. I need to change "As advised by SDS Team" to something else.
Finally after completing the above changes I need to be able to edit all the innertext of most of the nodes following <SDLT> with ease...so examples of how to accomplish this would also be appreciated.
Hope this is clear, thanks in advance for any help
Simon
<?xml version="1.0" encoding="UTF-8"?>
<!-- PLEASE NOTE - This is an example to show the layout of a valid XML
test message for an SDLT submission. V1.0 10 Jan 05 -->
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</Enve
<Header>
<MessageDetails>
<Class>IR-SDLT-LTR</Class>
<Qualifier>request</Qualif
<Function>submit</Function
<CorrelationID />
<Transformation>XML</Trans
<GatewayTest>1</GatewayTes
<GatewayTimestamp />
</MessageDetails>
<SenderDetails>
<IDAuthentication>
<SenderID>MSD2058</SenderI
<Authentication>
<Method>clear</Method>
<Role>principal</Role>
<Value>testing1</Value>
</Authentication>
</IDAuthentication>
</SenderDetails>
</Header>
<GovTalkDetails>
<Keys Type="1000002058">
<Key Type="STORN">As advised by SDS Team</Key>
</Keys>
<TargetDetails>
<Organisation>IR</Organisa
</TargetDetails>
<ChannelRouting>
<Channel>
<URI>2058</URI>
<Product>Themis</Product>
<Version>0.0.1</Version>
</Channel>
</ChannelRouting>
</GovTalkDetails>
<Body>
<IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/SDLT">
<IRheader>
<TestMessage>0</TestMessag
<Keys>
<Key Type="STORN">As advised by SDS Team</Key>
</Keys>
<PeriodEnd>2004-03-01</Per
<DefaultCurrency>GBP</Defa
<IRmark Type="generic">Include a valid IRmark here</IRmark>
<Sender>Individual</Sender
</IRheader>
<SDLT>
<UTRN>153492288MT</UTRN>
<TransactionDetails>
<TransactionDescription>F<
<EffectiveDate>2004-02-01<
<RestrictionsAffecting Apply="no">
</RestrictionsAffecting>
<ContractDate>2004-02-01</
<LandExchanged Exchanged="no">
</LandExchanged>
<PursuantToOption>no</Purs
<TaxCalculation>
<ClaimingRelief Claiming="no">
</ClaimingRelief>
<Consideration>
<TotalConsideration>100000
<FormCode>30</FormCode>
</Consideration>
<LinkedTransaction IsLinked="no">
</LinkedTransaction>
<TotalDue>1000.00</TotalDu
<AmountPaid IncludesPenalties="no">100
</TaxCalculation>
</TransactionDetails>
<LandDetail>
<NumberOfProperties>1</Num
<Property>
<PropertyType>01</Property
<AddressOfLand AddressExtended="no">
<PostCode>AA1 1AA</PostCode>
<HouseNumber>100</HouseNum
<Line>The Road</Line>
<Line>The Town</Line>
</AddressOfLand>
<LAnumber>9051</LAnumber>
<TitleNumber>AA11111</Titl
<NLPGUPRN>111111222222</NL
<PlanSubmitted>no</PlanSub
<InterestTransfered>FP</In
</Property>
</LandDetail>
<VendorDetails>
<NumberOfVendors>1</Number
<Vendor>
<Name>
<Title>Mr</Title>
<Forename>Tax</Forename>
<CompanyOrSurname>Payer</C
</Name>
<Address>
<PostCode>AA1 1AA</PostCode>
<HouseNumber>100</HouseNum
<Line>The Road</Line>
<Line>The Town</Line>
</Address>
</Vendor>
<AgentDetails>
<Name>Solicitor & Co</Name>
<Address>
<PostCode>BB1 1BB</PostCode>
<Line>The Office</Line>
<Line>The Road</Line>
<Line>The Town</Line>
</Address>
<DXnumber>11111 Town</DXnumber>
<EmailAddress>a@a.co.uk</E
<Reference>A1111/AA/AA</Re
<Telephone>01111 11111</Telephone>
</AgentDetails>
</VendorDetails>
<PurchaserDetails>
<NumberOfPurchasers>1</Num
<Purchaser>
<Name>
<Title>Miss</Title>
<Forename>Tax</Forename>
<CompanyOrSurname>Payer</C
</Name>
<SameAddressAsLand>yes</Sa
<NINO>AA111111A</NINO>
<Trustee>no</Trustee>
<VendorConnected>no</Vendo
<CertificateAddress>Agent<
<AuthoriseAgent>yes</Autho
<AgentDetails>
<Name>A Conveyancer</Name>
<Address>
<PostCode>AA1 2AB</PostCode>
<HouseNumber>1</HouseNumbe
<Line>A Road</Line>
<Line>A Town</Line>
</Address>
<DXnumber>22222 Town</DXnumber>
<Reference>A111/BB</Refere
<Telephone>02222 222222</Telephone>
</AgentDetails>
</Purchaser>
</PurchaserDetails>
<SupplementarySections>
<SDLT2Count>0</SDLT2Count>
<SDLT3Count>0</SDLT3Count>
<SDLT4Count>0</SDLT4Count>
</SupplementarySections>
</SDLT>
</IRenvelope>
</Body>
</GovTalkMessage>
ASKER
Sorry to be a pain but could you give me a VB.Net code example in the context of the XML I have and the points I made initially.
You should be able to do something like this:
doc.SelectSingleNode("//Go vTalkDetai ls/Keys/Ke y").InnerT ext = "Foo"
doc.SelectSingleNode("//Bo dy/IRenvel ope/IRhead er/Keys/Ke y").InnerT ext = "Bar"
doc.SelectSingleNode("//Go
doc.SelectSingleNode("//Bo
ASKER
Joe,
On the line of code you have given me used in this context I receieve the error "object reference not set to an instance of an object" any ideas?
Dim doc As XmlDocument = New XmlDocument
doc.Load("C:\sdlt.xml")
doc.SelectSingleNode("//Go vTalkDetai ls/Keys/Ke y").InnerT ext = "Foo"
doc.SelectSingleNode("//Bo dy/IRenvel ope/IRhead er/Keys/Ke y").InnerT ext = "Bar"
doc.Save("C:\newsdlt.xml")
On the line of code you have given me used in this context I receieve the error "object reference not set to an instance of an object" any ideas?
Dim doc As XmlDocument = New XmlDocument
doc.Load("C:\sdlt.xml")
doc.SelectSingleNode("//Go
doc.SelectSingleNode("//Bo
doc.Save("C:\newsdlt.xml")
I think you will get that error if the specified node doesn't exist. Obviously it does if the strings are correct. I typed them in by looking at your example but could easily have gotten one or both wrong. Check to make sure that I got the strings right if you just copied my code.
I use this technique a lot but I don't normally use attributes in my documents. You may have to include the attribute in the string as well
I use this technique a lot but I don't normally use attributes in my documents. You may have to include the attribute in the string as well
ASKER
The example XML code im using is exactly as per the layout you've got at the top but for some reason I still can't seem to get a handle on any of the nodes. For example as per the documentation that came with this XML it says that the <SenderID> could be accessed using the XPath "GovTalkMessage/Header/Sen derDetails /IDAuthent ication/Se nderID" .
So based on that I used the following...
doc.SelectSingleNode("GovT alkMessage /Header/Se nderDetail s/IDAuthen tication/S enderID"). InnerText = "Bar"
However I'm still getting object not set to an instance of an object. If I could get one working example of the above line of code based on the XML above I'd have enough to crack on with this...
So based on that I used the following...
doc.SelectSingleNode("GovT
However I'm still getting object not set to an instance of an object. If I could get one working example of the above line of code based on the XML above I'd have enough to crack on with this...
Try backing off to the top node:
doc.SelectSingleNode("GovT alkMessage ").InnerTe xt = "Bar"
or
doc.SelectSingleNode("//Go vTalkMessa ge").Inner Text = "Bar"
and see what you get.
doc.SelectSingleNode("GovT
or
doc.SelectSingleNode("//Go
and see what you get.
ASKER
How about this for weird...I used one of those programs that generates XPath queries for you (Stylus Studio XML Home Edition) and it gave me the following query to access \GovTalkMessage\Header\Mes sageDetail s\Class
"/*[local-name()='GovTalkM essage']/* [local-nam e()='Heade r'][1]/*[l ocal-name( )='Message Details'][ 1]/*[local -name()='C lass'][1]"
So I did the following...
doc.SelectSingleNode("/*[l ocal-name( )='GovTalk Message']/ *[local-na me()='Head er'][1]/*[ local-name ()='Messag eDetails'] [1]/*[loca l-name()=' Class'][1] ").InnerTe xt = "WOWOWO"
And sure enough it updated the InnerText no problems. However I tried the same program on a simple XML file, and it generated a simple Xpath query
"/bookstore/book[1]/title[ 1]"
Why does my XML (which although I can see is a bit more complicated isn't THAT bad) require such complicated Xpath query??
"/*[local-name()='GovTalkM
So I did the following...
doc.SelectSingleNode("/*[l
And sure enough it updated the InnerText no problems. However I tried the same program on a simple XML file, and it generated a simple Xpath query
"/bookstore/book[1]/title[
Why does my XML (which although I can see is a bit more complicated isn't THAT bad) require such complicated Xpath query??
This line: <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
includes a name space specification. The element names you see in the file are actually called "Local Names". There real names includes the name space as a prefix.
There is probably someway to set the namespace in XPath to avoid having to specify that you are serching only local names.
includes a name space specification. The element names you see in the file are actually called "Local Names". There real names includes the name space as a prefix.
There is probably someway to set the namespace in XPath to avoid having to specify that you are serching only local names.
Hi noel58,
It seems like you are wanting to pull all the SDLT info out of your db using vb.net, and insert it into your xml template doc? Without intending to rain on any strawberries, this is perhaps a flawed method from the start, and you may end up re-writting your app anyway.
My Reasoning:
- The IR are very picky about their XML structures, and what data is, and is not acceptable.
- I believe strings are output as utf-16, and the IR demand UTF-8 encoding.
- The IRmark node will have to be pulled out prior to processed, then reinserted back in with the relevant mark (..you tried creating one of them things yet??!)
etc etc etc... plus a hundred and one other reasons.
My advice?
Again I'm making assumptions.. you're not a commercial CMS provider from your previous posts by the looks...If your serious about writing this, start by exporting your data via an xslt.
If your patience (or time) is limited.. go commercial. Bolt on's to case management systems will be out soon (the IR went live this month).
Tara.
It seems like you are wanting to pull all the SDLT info out of your db using vb.net, and insert it into your xml template doc? Without intending to rain on any strawberries, this is perhaps a flawed method from the start, and you may end up re-writting your app anyway.
My Reasoning:
- The IR are very picky about their XML structures, and what data is, and is not acceptable.
- I believe strings are output as utf-16, and the IR demand UTF-8 encoding.
- The IRmark node will have to be pulled out prior to processed, then reinserted back in with the relevant mark (..you tried creating one of them things yet??!)
etc etc etc... plus a hundred and one other reasons.
My advice?
Again I'm making assumptions.. you're not a commercial CMS provider from your previous posts by the looks...If your serious about writing this, start by exporting your data via an xslt.
If your patience (or time) is limited.. go commercial. Bolt on's to case management systems will be out soon (the IR went live this month).
Tara.
ASKER
Tara,
Very insightful advice. I am a commercial CMS provider and what has got me is the lack of relevant support from the HMRC. No relevant examples (other than an outdated VB 6 application), just piles and piles of specifications, busines validation rules etc. I've asked them for a VB.Net example but nobody seems to be able to help me with that.
You mentioned bolt ons, who will be supplying these? The HMRC or are third parties now getting involved? Where would I find the IR bolt on?
I know what you mean about xslt, I've dabbled in that but its really not something I would choose to get stuck in to if there are alternatives such as bolt ons. The whole thing is a headache. We've integrated with search companies, form providers, and many other applications but this one has got me!
Simon
Very insightful advice. I am a commercial CMS provider and what has got me is the lack of relevant support from the HMRC. No relevant examples (other than an outdated VB 6 application), just piles and piles of specifications, busines validation rules etc. I've asked them for a VB.Net example but nobody seems to be able to help me with that.
You mentioned bolt ons, who will be supplying these? The HMRC or are third parties now getting involved? Where would I find the IR bolt on?
I know what you mean about xslt, I've dabbled in that but its really not something I would choose to get stuck in to if there are alternatives such as bolt ons. The whole thing is a headache. We've integrated with search companies, form providers, and many other applications but this one has got me!
Simon
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tara your comments have been most useful, I've decided to subcontract the whole job out to somebody who actually knows about XSLT and has the programming skills that I unfortunately don't!
Thanks for your help everyone.
Simon
Thanks for your help everyone.
Simon
In a weird way it's good to see someone having the same struggles I faced - it means I'm not the enept developer I was leading myself to beleive!
Best of luck,
Best of luck,
http://www.informit.com/articles/article.asp?p=23021&rl=1