Empty elements in XML

Last post 01-21-2007 12:53 PM by mpaterson. 2 replies.

Sort Posts:

  • Empty elements in XML

    01-21-2007, 11:18 AM
    • Contributor
      4,575 point Contributor
    • mpaterson
    • Member since 11-26-2006, 10:24 PM
    • Ipswich, MA
    • Posts 1,298

    Hi everyone,

    I'm building a windows app for work that will allow a user to upload an excel file and convert it's contents into XML by matching specific columns with specific XML elements.
    There is a very important requirement to allow for the generation of empty tags but I can't figure out how to do this.
    Following is the general flow that I need to successfully implement this app:

    <message ...namespace>
        <head>
           <messageID></messageID>
           <date></date>
           <messageType>orderImport</messageType>
        </head>
        <body>
          ..........
        </body>
    </message>

     

    As you can see above I could have several empty tags that MUST be there even if their values are null. 
    There may also be times when these elements will not be null.

    On a side note, once I enter data into the dataview to be displayed on my .xml file is there any way to "update" the .xml file if I modify the schema or data in the dataview?
    I found myself creating a new .xml file everytime I had to change something to make sure it works.

    Thanks!

    Michael

     

     

     

     

     

     

    If everything happens for a reason what is the reason for this error?
  • Re: Empty elements in XML

    01-21-2007, 12:23 PM
    Answer
    • Star
      9,676 point Star
    • pickyh3d
    • Member since 09-17-2002, 10:19 AM
    • Virginia
    • Posts 1,955

    Assuming you're creating the XML file yourself, then it's just a matter of creating the specified element and adding it as a child to its parent node with no inner value specified.

    xmlnRoot.AppendChild(xmlDoc.CreateElement("date"))

    That will create an empty date node ("<date />").  xmlnRoot is an XmlNode object and xmlDoc is an XmlDocument object.

    To do this in code, you could simply do:

    /// [C#]
    
    // create the date node
    //   I tend to create objects filled with the node names as constants,
    //   so that I can easily reference them in a fashion similar to how
    //   I am entering them.  Basically, XMLNames.Date would be "date"
    xmleChild = xmlDoc.CreateElement(XMLNames.Date);
    
    // if the string is null or empty, then use an empty node
    if ( ! string.IsNullOrEmpty(strDate) )
    {
        // set the text inside to the date
        xmleChild.InnerText = HttpUtility.HtmlEncode(strDate);
    }
    
    // add the date node
    xmlnRoot.AppendChild(xmleChild);
    
    ''' [VB.NET]
    
    ' create the date node
    '   I tend to create objects filled with the node names as constants,
    '   so that I can easily reference them in a fashion similar to how
    '   I am entering them.  Basically, XMLNames.DateNode would be "date"
    xmleChild = xmlDoc.CreateElement(XMLNames.DateNode)
    
    ' if the string is null or empty, then use an empty node
    If Not String.IsNullOrEmpty(strDate) Then
    
        ' set the text inside to the date
        xmleChild.InnerText = HttpUtility.HtmlEncode(strDate)
    
    End If
    
    ' add the date node
    xmlnRoot.AppendChild(xmleChild)

    Using the above code would allow you to check to see if the string is both null or empty, and leave the node blank, but still add it regardless. I'm not sure if you meant within Visual Studio or within your app with regards to the second question.

    Picky
  • Re: Empty elements in XML

    01-21-2007, 12:53 PM
    • Contributor
      4,575 point Contributor
    • mpaterson
    • Member since 11-26-2006, 10:24 PM
    • Ipswich, MA
    • Posts 1,298

    I will give this a try.  I'm really just adding the xml file to the project so I can validate my schema.
    From the looks of it my schema is not correct since when I add the xml file and populate a few values into the dataview, the elements associated with the null values aren't displayed.  I will be generating the xml once the excel file is uploaded but I didn't want to start that until I could validate the schema.

    Any ideas?

    Thanks

    If everything happens for a reason what is the reason for this error?
Page 1 of 1 (3 items)