XML Error help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vishal Bhavsar

    #1

    XML Error help

    Hello,

    I am fairly new to C# so bear with me if this is a simple question... I
    couldn't find much help searching for it on the web.
    Ok so I am trying to generate an XML document using the XmlTextWriter
    class. Here's the pseudo/code :


    XmlTextWriter writer = new XmlTextWriter(s tream, Encoding.ASCII) ;

    writer.WriteSta rtDocument(true );
    bool doOnce = true;

    foreach(String item in myArray)
    {
    if (doOnce == true)
    {
    writer.WriteSta rtElement("root ", "");
    writer.WriteAtt ributeString("r ootattribute", "1");
    doOnce = false;
    }

    writer.WriteSta rtElement("node ", "");

    writer.WriteEle mentString("nam e", "vishal");
    if (checkBox.Check ed)
    writer.WriteAtt ributeString("t ype", "admin");
    else
    writer.WriteAtt ributeString("t ype", "guest");

    if (checkBox2.Chec ked)
    {
    writer.WriteEle mentString("cou ntry", countryText.Tex t);
    }

    ... // more if statements of the same kind.

    writer.WriteEnd Element();

    }
    writer.WriteEnd Element();
    writer.WriteEnd Document();

    XmlDocument xd = new XmlDocument();
    xd.Load(saveStr eam);

    writer.Close();
    stream.Close();
    }


    (Sincere apologies if this code is hard to read due to formatting)
    This code compiles fine but the compiler points to an unhandled
    exception of type "System.Invalid OperationExcept ion" occurring at the
    line:
    if (checkBox2.Chec ked)
    and the additional information given is: "Token StartAttribute in state
    Content would result in an invalid XML document"

    I don't see any problems with the way the XMLElements are being formed.
    Maybe you can make me see what's wrong. Any help will be greatly
    appreciated!


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Jon Skeet

    #2
    Re: XML Error help

    Vishal Bhavsar <vish_devil@yah oo.com> wrote:[color=blue]
    > I am fairly new to C# so bear with me if this is a simple question... I
    > couldn't find much help searching for it on the web.
    > Ok so I am trying to generate an XML document using the XmlTextWriter
    > class. Here's the pseudo/code :[/color]

    <snip>
    [color=blue]
    > (Sincere apologies if this code is hard to read due to formatting)[/color]

    In future, it would be appreciated if rather than apologising for it,
    you'd fix it - if you fix it once, it saves several other people *each*
    fixing it. It would also have been helpful to make it a *complete*
    example that demonstrated the problem - see
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


    Anyway, the problem itself is that you can't write an attribute in the
    middle of thin air - you have to write it directly after starting an
    element. Essentially, by the time you've done:

    writer.WriteSta rtElement("node ", "");
    writer.WriteEle mentString("nam e", "vishal");

    you've output:

    <node>
    <name>vishal</name>

    so where would the attribute go?

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...