Best Practice Advice XML

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

    #1

    Best Practice Advice XML

    Hi,

    I'm needing to update an xml file by inserting a new node. First I need to
    load the xml into a XmlDocument from file.

    In the first run, the file won't exist and I will have to create a new
    XmlDocument. What is the best way to do this ?

    I'm thinking just catching an exception and building a new doc isn't the
    best way of doing this.

    If i check to see if the file exists, this is not neccessarily the best way
    since the file might be corrupt. I can program this, but I'm asking for
    advice on the best way of doing this. Can anyone advise me?

    public void savePlayer()

    {

    XmlDocument xmldoc = new XmlDocument();

    XmlNode members;

    try

    {

    StreamReader xmlfile = new StreamReader("m embers.xml");

    xmldoc.Load(xml file);

    xmlfile.Close() ;

    //get the members node

    members = xmldoc.Document Element;

    }

    catch

    {

    //the file doesn't exist so we must create a new document

    //let's add the XML declaration section

    XmlNode xmlnode=xmldoc. CreateNode(XmlN odeType.XmlDecl aration,"","");

    xmldoc.AppendCh ild(xmlnode);


    //let's add the node element

    members = xmldoc.CreateEl ement("members" );

    xmldoc.AppendCh ild(members);

    }

    XmlNode member = xmldoc.CreateEl ement("member") ;





    //the display name

    XmlElement displayname = xmldoc.CreateEl ement("displayn ame");

    XmlText displayname_val ue = xmldoc.CreateTe xtNode("value") ;

    displayname.App endChild(displa yname_value);

    member.AppendCh ild(displayname ); //add the username to the player


    members.AppendC hild(player);

    xmldoc.Save("me mbers.xml");

    }


  • seani

    #2
    Re: Best Practice Advice XML

    On Mon, 23 May 2005 17:33:05 +0100, Fraser wrote:
    [color=blue]
    > Hi,
    >
    > I'm needing to update an xml file by inserting a new node. First I need
    > to load the xml into a XmlDocument from file.
    >
    > In the first run, the file won't exist and I will have to create a new
    > XmlDocument. What is the best way to do this ?
    >
    > I'm thinking just catching an exception and building a new doc isn't the
    > best way of doing this.
    >[/color]

    And I'd suggest that your instinct is correct. The fact that a file
    doesn't exist *is* an "exception" al condition; exceptions are suitable for
    this task. The mindset that exception===err or is just dogma.

    However:

    1) Ensure you only catch ans suitably code for your
    "file-does-not-exist" exception, rather than every exception.

    2) You'll need to cater for corrupt files whatever.

    [color=blue]
    > If i check to see if the file exists, this is not neccessarily the[/color]
    best[color=blue]
    > way since the file might be corrupt. I can program this, but I'm asking
    > for advice on the best way of doing this. Can anyone advise me?
    >
    >[sample code snipped][/color]

    Comment

    • seani

      #3
      Re: Best Practice Advice XML

      On Tue, 24 May 2005 00:22:44 +0200, seani wrote:
      [color=blue]
      > On Mon, 23 May 2005 17:33:05 +0100, Fraser wrote:
      >[color=green]
      >> Hi,
      >>
      >> I'm needing to update an xml file by inserting a new node. First I need
      >> to load the xml into a XmlDocument from file.
      >>
      >> In the first run, the file won't exist and I will have to create a new
      >> XmlDocument. What is the best way to do this ?
      >>
      >> I'm thinking just catching an exception and building a new doc isn't the
      >> best way of doing this.
      >>
      >>[/color]
      > And I'd suggest that your instinct is correct. The fact that a file
      > doesn't exist *is* an "exception" al condition; exceptions are suitable for
      > this task. The mindset that exception===err or is just dogma.
      >[/color]

      Oops-a-daisy, I meant to say "your instinct is *incorrect*".

      The rest of my post may seem more consistent now.

      Comment

      Working...