xml format problem

Discussion in 'ASP .Net' started by CindyH, May 20, 2008.

  1. CindyH

    CindyH Guest

    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
     
    CindyH, May 20, 2008
    #1
    1. Advertising

  2. "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
     
    Anthony Jones, May 20, 2008
    #2
    1. Advertising

  3. CindyH

    CindyH Guest

    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
    >
    >
    >
     
    CindyH, May 20, 2008
    #3
  4. CindyH

    CindyH Guest

    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
    >
    >
    >
     
    CindyH, May 20, 2008
    #4
  5. "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
     
    Anthony Jones, May 20, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,169
    Janaka
    Feb 18, 2004
  2. Replies:
    2
    Views:
    430
    TextDoctor
    May 7, 2005
  3. Replies:
    1
    Views:
    381
  4. Erik Wasser
    Replies:
    5
    Views:
    463
    Peter J. Holzer
    Mar 5, 2006
  5. Binaryx
    Replies:
    3
    Views:
    5,109
    Peter Flynn
    Apr 14, 2012
Loading...

Share This Page