How to set a default value for attribute if it doesn't exist in the XDocument object

Marco Dinatsoli

I am trying to load an xml file. I did this:

from e in XDocument.Load(stream).Root.Elements("cust")
                            select new Customer
                            {
                                MemeberID = (int)e.Attribute("custid"),
                                CustomerID = (int)e.Attribute("custid"),
                                FirstName = (string)e.Attribute("fname"),
                                LastName = (string)e.Attribute("lname"),
                                ShowsNumber = (int)e.Attribute("count_noshow"),
                                VisitNumber = (int)e.Attribute("count_resos"),
                                Cancellation = (int)e.Attribute("count_cancel"),
                                MobileNumber = (string)e.Element("phone").Attribute("phonenumber")
                            })

even thing was working good, but now i have a situation in which the xml document is not neccessary to have the mobilenuber attribute. so can I set a default value for this mobile number if it wasn't there in the xml node?

many thanks

supertopi

Attribute not required:

MobileNumber = (string)e.Element("phone").Attribute("phonenumber") ?? defaultValue

Element not required:

MobileNumber = e.Element("phone") != null ? (string)e.Element("phone").Attribute("phonenumber") : defaultValue

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set a default value for attribute if it doesn't exist in the XDocument object

From Dev

How to set an RSpec expectation for an object that doesn't exist yet

From Dev

Default value if it doesn't exist in map

From Dev

How to set the default value of directive attribute?

From Dev

throw error when attribute is accessed but doesn't exist in object

From Dev

How to set null if relations doesn't exist?

From Dev

Remove item from Object set that doesn't exist in query set

From Dev

Postgres return a default value when a column doesn't exist

From Dev

JSON deserialization in scala - use default value if field doesn't exist

From Dev

MySql use default value if searched doesn't exist

From Dev

Calculate attribute if it doesn't exist

From Dev

Default value in object property if not exist

From Dev

how to structure loop to check if object value doesn't exist in nested array

From Dev

How to show default "image" if the loaded image doesn't exist?

From Dev

How to set a default attribute value for a Laravel / Eloquent model?

From Dev

Java: how to set default value to annotation with another annotation as its attribute?

From Dev

VertexArrayObject, how can i set "default" attribute value?

From Dev

SQL - SET DEFAULT doesn't work when deleting a value

From Dev

Determining if Swift dictionary doesn't contains a key and set default value

From Dev

How do I set an env variable in PowerShell if it doesn't exist?

From Dev

jQuery - How to set a select which doesn't exist yet

From Dev

jQuery - How to set a select which doesn't exist yet

From Dev

Attribute [PXNotPersistable] doesn't exist in version 6.1

From Dev

MongoDB and mongoose: how to add an object if it doesn't already exist

From Dev

Python - os.path doesn't exist: AttributeError: 'module' object has no attribute 'path'

From Dev

Can't set empty object as default value for param with type Record

From Dev

How to select a specific node from XDocument having a specific attribute value?

From Dev

How to select a specific node from XDocument having a specific attribute value?

From Dev

Mysql set default value if entry does not exist

Related Related

  1. 1

    How to set a default value for attribute if it doesn't exist in the XDocument object

  2. 2

    How to set an RSpec expectation for an object that doesn't exist yet

  3. 3

    Default value if it doesn't exist in map

  4. 4

    How to set the default value of directive attribute?

  5. 5

    throw error when attribute is accessed but doesn't exist in object

  6. 6

    How to set null if relations doesn't exist?

  7. 7

    Remove item from Object set that doesn't exist in query set

  8. 8

    Postgres return a default value when a column doesn't exist

  9. 9

    JSON deserialization in scala - use default value if field doesn't exist

  10. 10

    MySql use default value if searched doesn't exist

  11. 11

    Calculate attribute if it doesn't exist

  12. 12

    Default value in object property if not exist

  13. 13

    how to structure loop to check if object value doesn't exist in nested array

  14. 14

    How to show default "image" if the loaded image doesn't exist?

  15. 15

    How to set a default attribute value for a Laravel / Eloquent model?

  16. 16

    Java: how to set default value to annotation with another annotation as its attribute?

  17. 17

    VertexArrayObject, how can i set "default" attribute value?

  18. 18

    SQL - SET DEFAULT doesn't work when deleting a value

  19. 19

    Determining if Swift dictionary doesn't contains a key and set default value

  20. 20

    How do I set an env variable in PowerShell if it doesn't exist?

  21. 21

    jQuery - How to set a select which doesn't exist yet

  22. 22

    jQuery - How to set a select which doesn't exist yet

  23. 23

    Attribute [PXNotPersistable] doesn't exist in version 6.1

  24. 24

    MongoDB and mongoose: how to add an object if it doesn't already exist

  25. 25

    Python - os.path doesn't exist: AttributeError: 'module' object has no attribute 'path'

  26. 26

    Can't set empty object as default value for param with type Record

  27. 27

    How to select a specific node from XDocument having a specific attribute value?

  28. 28

    How to select a specific node from XDocument having a specific attribute value?

  29. 29

    Mysql set default value if entry does not exist

HotTag

Archive