locked
XDocument loads all data into root value RRS feed

  • Question

  • I have an XML document that I am trying to load.  It loads all data without the elements into the Root value under locals window.  This is a windows store app C#.

    Here is a sample of xml file:

    <?xml version="1.0" encoding="utf-8"?> <ManholeMap> <Manhole> <CityName>removed</CityName> <Basin>11</Basin> <PAManholeID>3368</PAManholeID> <PAType>MH</PAType> <Address>3600 WEST BROADWAY</Address> <AreaPhotoFile>3368A0044.jpg</AreaPhotoFile> <AssetDiameter>Std 48 Round</AssetDiameter> <AssetGrade>Above Grade</AssetGrade> <AssetInformationKey>10-264#11#3368#11/5/2010 6:32:16 PM#1</AssetInformationKey> <AssetMaterial>Concrete</AssetMaterial> <AssetType>Manhole</AssetType> <CityManholeID></CityManholeID> <Comments></Comments> <Crew>EJR</Crew> <DebrisDepth>0</DebrisDepth> <GradeInch>9</GradeInch> <InflowPotential>None</InflowPotential> <InspectionDate>11/5/2010</InspectionDate> <InspectionKey>10-264#11#3368#11/5/2010 6:32:16 PM</InspectionKey> <InspectionStatus>Yes</InspectionStatus> <InternalPhotoFile>3368I0045.jpg</InternalPhotoFile> <LidType>Vented</LidType> <MapKey>Ardmore OK#3368</MapKey> <OutgoingPipeDepth>4.60</OutgoingPipeDepth> <OID>1</OID> <SubBasin>NA</SubBasin> <SurfaceCover>Road Shoulder</SurfaceCover> <SurfaceMaterial>Unpaved</SurfaceMaterial> <Vaultsize></Vaultsize> <Geometry>POINT (-97.176705394748339 34.172532521336649)</Geometry> </Manhole> <Manhole> <CityName>removed</CityName> <Basin>11</Basin> <PAManholeID>5414</PAManholeID> <PAType>MH</PAType> <Address>1100 RIDGEWAY</Address> <AreaPhotoFile>5414A0008.jpg</AreaPhotoFile> <AssetDiameter>Std 48 Round</AssetDiameter> <AssetGrade>Below Grade</AssetGrade> <AssetInformationKey>10-264#11#5414#10/20/2010 3:55:50 PM#1</AssetInformationKey> <AssetMaterial>Concrete</AssetMaterial> <AssetType>Manhole</AssetType> <CityManholeID></CityManholeID> <Comments></Comments> <Crew>ER</Crew> <DebrisDepth>1</DebrisDepth> <GradeInch>1</GradeInch> <InflowPotential>Severe</InflowPotential> <InspectionDate>10/20/2010</InspectionDate> <InspectionKey>10-264#11#5414#10/20/2010 3:55:50 PM</InspectionKey> <InspectionStatus>Yes</InspectionStatus> <InternalPhotoFile>5414I0009.jpg</InternalPhotoFile> <LidType>Solid</LidType> <MapKey>Ardmore OK#5414</MapKey> <OutgoingPipeDepth>7.68</OutgoingPipeDepth> <OID>2</OID> <SubBasin>NA</SubBasin> <SurfaceCover>Yard</SurfaceCover> <SurfaceMaterial>Unpaved</SurfaceMaterial> <Vaultsize></Vaultsize> <Geometry>POINT (-97.156172211495161 34.160821031744987)</Geometry> </Manhole> </ManholeMap>

           

    private async void ButtonLoadMapData_Click(object sender, RoutedEventArgs e) { var picker = new Windows.Storage.Pickers.FileOpenPicker(); picker.FileTypeFilter.Add(".xml"); var file = await picker.PickSingleFileAsync(); if (file != null) { try { var folder = await StorageFile.GetFileFromPathAsync(file.Path); var stream = await folder.OpenStreamForReadAsync() as Stream; XDocument mapDoc = XDocument.Load(stream); ManholeMapViewModel mapManhole = new ManholeMapViewModel(); List<ManholeMapViewModel> listMapManhole = (from _mapManhole in mapDoc.Element("ManholeMap").Elements("Manhole") select new ManholeMapViewModel { Address = _mapManhole.Element("Address").Value, CityName = _mapManhole.Element("CityName").Value, Basin = _mapManhole.Element("Basin").Value, PAManholeID = _mapManhole.Element("PAManholeID").Value, PAType = _mapManhole.Element("PAType").Value, AreaPhotoFile = _mapManhole.Element("AreaPhotoFile").Value, AssetDiameter = _mapManhole.Element("AssetDiameter").Value, AssetGrade = _mapManhole.Element("AssetGrade").Value, AssetInformationKey = _mapManhole.Element("AssetInformationKey").Value, AssetMaterial = _mapManhole.Element("AssetMaterial").Value, AssetType = _mapManhole.Element("AssetType").Value, CityManholeID = _mapManhole.Element("CityManholeID").Value, Comments = _mapManhole.Element("Comments").Value, Crew = _mapManhole.Element("Crew").Value, DebrisDepth = _mapManhole.Element("DebrisDepth").Value, Geometry = _mapManhole.Element("Geometry").Value, GradeInch = _mapManhole.Element("GradeInch").Value, InflowPotential = _mapManhole.Element("InflowPotential").Value, InspectionDate = _mapManhole.Element("InspectionDate").Value, InspectionKey = _mapManhole.Element("InspectionKey").Value, InspectionStatus = _mapManhole.Element("InspectionStatus").Value, InternalPhotoFile = _mapManhole.Element("InternalPhotoFile").Value, LidType = _mapManhole.Element("LidType").Value, MapKey = _mapManhole.Element("MapKey").Value, OID = _mapManhole.Element("OID").Value, OutgoingPipeDepth = _mapManhole.Element("OutgoingPipeDepth").Value, SubBasin = _mapManhole.Element("SubBasin").Value, SurfaceCover = _mapManhole.Element("SurfaceCover").Value, SurfaceMaterial = _mapManhole.Element("SurfaceMaterial").Value, VaultSize = _mapManhole.Element("VaultSize").Value } ).ToList(); foreach (var _manhole in listMapManhole) { string s = string.Empty; } await stream.FlushAsync(); } catch (Exception ex) { string s = ex.Message; } finally { } } }

    When I run the code it gives null reference exception when it gets to the LINQ code.  Any help or suggestions would be appreciated.  Thanks

    Friday, January 17, 2014 1:32 PM

Answers

  • I found the answer.  In the xml file the element <Vaultsize> should have been <VaultSize>.  When I changed the s to a capital S the code works fine.  In the LINQ query I had it as VaultSize.  They both need to match the case.
    • Marked as answer by Razlak Saturday, January 18, 2014 12:06 PM
    Saturday, January 18, 2014 12:01 PM

All replies

  • Sure, can you show the rest of the code, or even better, give a place where we can download a project to test?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, January 17, 2014 4:27 PM
    Moderator
  • Hello Raxlak, 

    try this type code :

    string tex = await FileIO.ReadTextAsync(file);
    XDocument xd = XDocument.Parse(tex);
    var data = from item in xd.Descendants("Manhole")
                               select new
                              {
    Address = _mapManhole.Element("Address").Value,
    CityName = _mapManhole.Element("CityName").Value,
    Basin = _mapManhole.Element("Basin").Value,
    PAManholeID = _mapManhole.Element("PAManholeID").Value,
    PAType = _mapManhole.Element("PAType").Value,
    AreaPhotoFile = _mapManhole.Element("AreaPhotoFile").Value,
    AssetDiameter = _mapManhole.Element("AssetDiameter").Value,
    AssetGrade = _mapManhole.Element("AssetGrade").Value,
    AssetInformationKey = _mapManhole.Element("AssetInformationKey").Value,
    AssetMaterial = _mapManhole.Element("AssetMaterial").Value,
    AssetType = _mapManhole.Element("AssetType").Value,
    CityManholeID = _mapManhole.Element("CityManholeID").Value,
    Comments = _mapManhole.Element("Comments").Value,
    Crew = _mapManhole.Element("Crew").Value,
    DebrisDepth = _mapManhole.Element("DebrisDepth").Value,
    Geometry = _mapManhole.Element("Geometry").Value,
    GradeInch = _mapManhole.Element("GradeInch").Value,
    InflowPotential = _mapManhole.Element("InflowPotential").Value,
    InspectionDate = _mapManhole.Element("InspectionDate").Value,
    InspectionKey = _mapManhole.Element("InspectionKey").Value,
    InspectionStatus = _mapManhole.Element("InspectionStatus").Value,
    InternalPhotoFile = _mapManhole.Element("InternalPhotoFile").Value,
    LidType = _mapManhole.Element("LidType").Value,
    MapKey = _mapManhole.Element("MapKey").Value,
    OID = _mapManhole.Element("OID").Value,
    OutgoingPipeDepth = _mapManhole.Element("OutgoingPipeDepth").Value,
    SubBasin = _mapManhole.Element("SubBasin").Value,
    SurfaceCover = _mapManhole.Element("SurfaceCover").Value,
    SurfaceMaterial = _mapManhole.Element("SurfaceMaterial").Value,
    VaultSize = _mapManhole.Element("VaultSize").Value                                                                
    }


    Saturday, January 18, 2014 7:45 AM
  • I found the answer.  In the xml file the element <Vaultsize> should have been <VaultSize>.  When I changed the s to a capital S the code works fine.  In the LINQ query I had it as VaultSize.  They both need to match the case.
    • Marked as answer by Razlak Saturday, January 18, 2014 12:06 PM
    Saturday, January 18, 2014 12:01 PM