views:

696

answers:

1

I'm working on a .NET application (VB 2008) that gets all of its data from a web-based Django application.

I'm using Linq-to-XML and Linq-to-objects in the app.

One API call is giving me XML data in the following format that I am loading into an XDocument.

<django-objects version="1.0">
    <object pk="1" model="app.person">
        <field type="CharField" name="reference_num">001</field>
     <field to="app.office" name="office" rel="ManyToOneRel">2</field>
     <field type="DateField" name="date">2008-09-03</field>
     <field type="CharField" name="surname"></field>
     <field type="CharField" name="name">Sandra</field>
     <field type="DateField" name="birthdate">1988-11-03</field>
     <field type="CharField" name="sex">F</field>
     <field type="CharField" name="marital_status">S</field>
     <field type="TextField" name="special_interests">Biking</field>
     <field type="CharField" name="identity_proof"></field>
     <field type="CharField" name="identity_proof_number"></field>
     <field type="BooleanField" name="is_active">0</field>
     <field type="DateTimeField" name="created">2008-09-03 12:46:39</field>
     <field type="DateTimeField" name="modified">2008-09-03 12:46:39</field>
     <field type="DateField" name="important_date"><None></None></field>
     <field type="DateField" name="archive_date"><None></None></field>
     <field type="CharField" name="process_status"></field>
     <field type="FileField" name="image"></field>
     <field to="auth.user" name="self_user" rel="OneToOneRel">13</field>
    </object>
    <object pk="2" model="app.person">
    .
    <!-- more similar xml //-->
    .
    </object>
    .
    .
    .
</django-objects>

This is the format, the API call returns several <object> elements like this one. This XML contains <None></None> to represent Null values, for example, towards the bottom in the code block above, in <field type="DateField" name="archive_date"><None></None></field> . The <None></None> tells me that in the database this field has a null value. Other <object> elements in this XDocument may have valid dates or <None></None> . What I would like to do is: for all <object> where <field type="DateField" name="archive_date"> contains <None></None> replace this null representation with 1899-01-01.

How would I go about achieving this?

Thanks.

+1  A: 

What about something like this?

XElement djangoElement = XElement.Load(@"c:\django.xml");

IEnumerable<XElement> archiveDateElements = from fieldElement in djangoElement.Elements("object").Elements("field")
                                            where fieldElement.Attribute("type") != null && 
                                                fieldElement.Attribute("type").Value == "DateField" &&
                                                fieldElement.Attribute("name") != null &&
                                                fieldElement.Attribute("name").Value == "archive_date"
                                     select fieldElement;

foreach (var archiveDateElement in archiveDateElements)
{
    XElement noneElement = archiveDateElement.Element("None");

    if (noneElement != null)
    {
        noneElement.ReplaceWith("1899-01-01");
    }
}

P.S. Sorry - just noticed it was VB.NET you were after. But I guess the similarities are such that it may point you in the right direction.

dommer
You set me in the right direction. I eventually used a simpler where clause, since all `<fields>` have the "name" attribute. Thanks. From fieldElement In doc.Descendants("object").Elements("field") Where fieldElement.Attribute("name").Value = "archive_date" Select fieldElement
chefsmart