Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > xml format problem

Reply
Thread Tools

xml format problem

 
 
CindyH
Guest
Posts: n/a
 
      05-20-2008
Hi

I'm using the following code to create xml string:

Dim Doc As New System.Xml.XmlDocument
Dim newAtt As System.Xml.XmlAttribute

Dim dec As System.Xml.XmlDeclaration
dec = Doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
dec.Encoding = "UTF-8"
Doc.AppendChild(dec)

Dim DocRoot As System.Xml.XmlElement = Doc.CreateElement("userlist")
newAtt = Doc.CreateAttribute("ACTION")
newAtt.Value = vAction
DocRoot.Attributes.Append(newAtt)

newAtt = Doc.CreateAttribute("VENDORNAME")
newAtt.Value = vVendorName
DocRoot.Attributes.Append(newAtt)
Doc.AppendChild(DocRoot)

Dim amouser As System.Xml.XmlNode = Doc.CreateElement("amouser")
newAtt = Doc.CreateAttribute("AMOAID")
newAtt.Value = vAMOAID
amouser.Attributes.Append(newAtt)

newAtt = Doc.CreateAttribute("VENDORUSERNAME")
newAtt.Value = vH2UserName
amouser.Attributes.Append(newAtt)

newAtt = Doc.CreateAttribute("AMOATOKEN")
newAtt.Value = vAMOAToken
amouser.Attributes.Append(newAtt)

DocRoot.AppendChild(amouser)

Dim xmlstring = Doc.OuterXml



The result looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<userlist ACTION="redirectuser" VENDORNAME="H2Digital">
<amouser AMOAID="bb224c2a-fe8a-4c3f-acf4-6c0986b8cf78"
VENDORUSERNAME=""
AMOATOKEN="hx0gH6e8PvswEzaA8oXPoVIY/KvnbP2/" />
</userlist>


I need the result to look like this: with </amouser> instead of />

<?xml version="1.0" encoding="UTF-8"?>
<userlist ACTION="redirectuser" VENDORNAME="H2Digital">
<amouser AMOAID="bb224c2a-fe8a-4c3f-acf4-6c0986b8cf78"
VENDORUSERNAME=""
AMOATOKEN="hx0gH6e8PvswEzaA8oXPoVIY/KvnbP2/" </amouser>
</userlist>

Does anyone know what I'm doing wrong here?
Thanks,
Cindy


 
Reply With Quote
 
 
 
 
Anthony Jones
Guest
Posts: n/a
 
      05-20-2008
"CindyH" <> wrote in message
news:...
> Hi
>
> I'm using the following code to create xml string:
>
> Dim Doc As New System.Xml.XmlDocument
> Dim newAtt As System.Xml.XmlAttribute
>
> Dim dec As System.Xml.XmlDeclaration
> dec = Doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
> dec.Encoding = "UTF-8"
> Doc.AppendChild(dec)
>
> Dim DocRoot As System.Xml.XmlElement = Doc.CreateElement("userlist")
> newAtt = Doc.CreateAttribute("ACTION")
> newAtt.Value = vAction
> DocRoot.Attributes.Append(newAtt)
>
> newAtt = Doc.CreateAttribute("VENDORNAME")
> newAtt.Value = vVendorName
> DocRoot.Attributes.Append(newAtt)
> Doc.AppendChild(DocRoot)
>
> Dim amouser As System.Xml.XmlNode = Doc.CreateElement("amouser")
> newAtt = Doc.CreateAttribute("AMOAID")
> newAtt.Value = vAMOAID
> amouser.Attributes.Append(newAtt)
>
> newAtt = Doc.CreateAttribute("VENDORUSERNAME")
> newAtt.Value = vH2UserName
> amouser.Attributes.Append(newAtt)
>
> newAtt = Doc.CreateAttribute("AMOATOKEN")
> newAtt.Value = vAMOAToken
> amouser.Attributes.Append(newAtt)
>
> DocRoot.AppendChild(amouser)
>
> Dim xmlstring = Doc.OuterXml
>
>
>
> The result looks like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <userlist ACTION="redirectuser" VENDORNAME="H2Digital">
> <amouser AMOAID="bb224c2a-fe8a-4c3f-acf4-6c0986b8cf78"
> VENDORUSERNAME=""
> AMOATOKEN="hx0gH6e8PvswEzaA8oXPoVIY/KvnbP2/" />
> </userlist>
>
>
> I need the result to look like this: with </amouser> instead of />
>
> <?xml version="1.0" encoding="UTF-8"?>
> <userlist ACTION="redirectuser" VENDORNAME="H2Digital">
> <amouser AMOAID="bb224c2a-fe8a-4c3f-acf4-6c0986b8cf78"
> VENDORUSERNAME=""
> AMOATOKEN="hx0gH6e8PvswEzaA8oXPoVIY/KvnbP2/" </amouser>
> </userlist>
>
> Does anyone know what I'm doing wrong here?


Your not doing anything wrong the output is correct. I can't think why you
need to specifically have a closing tag rather than the short form /> but if
you do:-

amouser.InnerText = ""

BTW use import of System.Xml to eliminate the long type names.

Also why not simply use the elements .SetAttribute method to create the
attributes instead of creating them as nodes, code would look a lot simpler

--
Anthony Jones - MVP ASP/ASP.NET



 
Reply With Quote
 
 
 
 
CindyH
Guest
Posts: n/a
 
      05-20-2008
Can you give me an example of how to use the .SetAttribute
in my code?


"Anthony Jones" <> wrote in message
news:...
> "CindyH" <> wrote in message
> news:...
>> Hi
>>
>> I'm using the following code to create xml string:
>>
>> Dim Doc As New System.Xml.XmlDocument
>> Dim newAtt As System.Xml.XmlAttribute
>>
>> Dim dec As System.Xml.XmlDeclaration
>> dec = Doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
>> dec.Encoding = "UTF-8"
>> Doc.AppendChild(dec)
>>
>> Dim DocRoot As System.Xml.XmlElement = Doc.CreateElement("userlist")
>> newAtt = Doc.CreateAttribute("ACTION")
>> newAtt.Value = vAction
>> DocRoot.Attributes.Append(newAtt)
>>
>> newAtt = Doc.CreateAttribute("VENDORNAME")
>> newAtt.Value = vVendorName
>> DocRoot.Attributes.Append(newAtt)
>> Doc.AppendChild(DocRoot)
>>
>> Dim amouser As System.Xml.XmlNode = Doc.CreateElement("amouser")
>> newAtt = Doc.CreateAttribute("AMOAID")
>> newAtt.Value = vAMOAID
>> amouser.Attributes.Append(newAtt)
>>
>> newAtt = Doc.CreateAttribute("VENDORUSERNAME")
>> newAtt.Value = vH2UserName
>> amouser.Attributes.Append(newAtt)
>>
>> newAtt = Doc.CreateAttribute("AMOATOKEN")
>> newAtt.Value = vAMOAToken
>> amouser.Attributes.Append(newAtt)
>>
>> DocRoot.AppendChild(amouser)
>>
>> Dim xmlstring = Doc.OuterXml
>>
>>
>>
>> The result looks like this:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <userlist ACTION="redirectuser" VENDORNAME="H2Digital">
>> <amouser AMOAID="bb224c2a-fe8a-4c3f-acf4-6c0986b8cf78"
>> VENDORUSERNAME=""
>> AMOATOKEN="hx0gH6e8PvswEzaA8oXPoVIY/KvnbP2/" />
>> </userlist>
>>
>>
>> I need the result to look like this: with </amouser> instead of />
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <userlist ACTION="redirectuser" VENDORNAME="H2Digital">
>> <amouser AMOAID="bb224c2a-fe8a-4c3f-acf4-6c0986b8cf78"
>> VENDORUSERNAME=""
>> AMOATOKEN="hx0gH6e8PvswEzaA8oXPoVIY/KvnbP2/" </amouser>
>> </userlist>
>>
>> Does anyone know what I'm doing wrong here?

>
> Your not doing anything wrong the output is correct. I can't think why
> you
> need to specifically have a closing tag rather than the short form /> but
> if
> you do:-
>
> amouser.InnerText = ""
>
> BTW use import of System.Xml to eliminate the long type names.
>
> Also why not simply use the elements .SetAttribute method to create the
> attributes instead of creating them as nodes, code would look a lot
> simpler
>
> --
> Anthony Jones - MVP ASP/ASP.NET
>
>
>



 
Reply With Quote
 
CindyH
Guest
Posts: n/a
 
      05-20-2008
amouser.InnerText = ""
This worked by the way - thanks!


"Anthony Jones" <> wrote in message
news:...
> "CindyH" <> wrote in message
> news:...
>> Hi
>>
>> I'm using the following code to create xml string:
>>
>> Dim Doc As New System.Xml.XmlDocument
>> Dim newAtt As System.Xml.XmlAttribute
>>
>> Dim dec As System.Xml.XmlDeclaration
>> dec = Doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
>> dec.Encoding = "UTF-8"
>> Doc.AppendChild(dec)
>>
>> Dim DocRoot As System.Xml.XmlElement = Doc.CreateElement("userlist")
>> newAtt = Doc.CreateAttribute("ACTION")
>> newAtt.Value = vAction
>> DocRoot.Attributes.Append(newAtt)
>>
>> newAtt = Doc.CreateAttribute("VENDORNAME")
>> newAtt.Value = vVendorName
>> DocRoot.Attributes.Append(newAtt)
>> Doc.AppendChild(DocRoot)
>>
>> Dim amouser As System.Xml.XmlNode = Doc.CreateElement("amouser")
>> newAtt = Doc.CreateAttribute("AMOAID")
>> newAtt.Value = vAMOAID
>> amouser.Attributes.Append(newAtt)
>>
>> newAtt = Doc.CreateAttribute("VENDORUSERNAME")
>> newAtt.Value = vH2UserName
>> amouser.Attributes.Append(newAtt)
>>
>> newAtt = Doc.CreateAttribute("AMOATOKEN")
>> newAtt.Value = vAMOAToken
>> amouser.Attributes.Append(newAtt)
>>
>> DocRoot.AppendChild(amouser)
>>
>> Dim xmlstring = Doc.OuterXml
>>
>>
>>
>> The result looks like this:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <userlist ACTION="redirectuser" VENDORNAME="H2Digital">
>> <amouser AMOAID="bb224c2a-fe8a-4c3f-acf4-6c0986b8cf78"
>> VENDORUSERNAME=""
>> AMOATOKEN="hx0gH6e8PvswEzaA8oXPoVIY/KvnbP2/" />
>> </userlist>
>>
>>
>> I need the result to look like this: with </amouser> instead of />
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <userlist ACTION="redirectuser" VENDORNAME="H2Digital">
>> <amouser AMOAID="bb224c2a-fe8a-4c3f-acf4-6c0986b8cf78"
>> VENDORUSERNAME=""
>> AMOATOKEN="hx0gH6e8PvswEzaA8oXPoVIY/KvnbP2/" </amouser>
>> </userlist>
>>
>> Does anyone know what I'm doing wrong here?

>
> Your not doing anything wrong the output is correct. I can't think why
> you
> need to specifically have a closing tag rather than the short form /> but
> if
> you do:-
>
> amouser.InnerText = ""
>
> BTW use import of System.Xml to eliminate the long type names.
>
> Also why not simply use the elements .SetAttribute method to create the
> attributes instead of creating them as nodes, code would look a lot
> simpler
>
> --
> Anthony Jones - MVP ASP/ASP.NET
>
>
>



 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      05-20-2008
"CindyH" <> wrote in message
news:...
> Can you give me an example of how to use the .SetAttribute
> in my code?
>
>


Hows this :-


Imports System.Xml

....

Dim Doc As New XmlDocument

Dim dec As System.Xml.XmlDeclaration
dec = Doc.CreateXmlDeclaration("1.0", Nothing, Nothing)
dec.Encoding = "UTF-8"
Doc.AppendChild(dec)

Dim DocRoot As XmlElement = Doc.CreateElement("userlist")
Doc.AppendChild(DocRoot)

DocRoot.SetAttribute("ACTION", vAction)
DocRoot.SetAttribute("VENDORNAME", vVendorName)

Dim amouser As XmlElement = Doc.CreateElement("amouser")
DocRoot.AppendChild(amouser)

amouser .SetAttribute("AMOAID", vAMOAID)
amouser .SetAttribute("VENDORUSERNAME", vH2UserName)
amouser .SetAttribute("AMOATOKEN", vAMOAToken)

Dim xmlstring = Doc.OuterXml


--
Anthony Jones - MVP ASP/ASP.NET


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML to another XML format transformation using XSLT Binaryx XML 3 04-14-2012 10:20 PM
Want help on how we convert output to tabular format Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. sharan XML 1 10-26-2007 01:20 PM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
how to convert XML document to several XML documents in a new format enyetor@gmail.com XML 2 05-07-2005 07:22 PM
how to convert XML document to several XML documents in a new format enyetor@gmail.com XML 1 04-26-2005 08:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57