Exception C# null list - c#

I am filling a list from an XML file. Some nodes may not exist and this causes an exception because it returns null. Here is the code:
public static List<Compte> getXmlComptes(string pathXml)
{
var doc = XDocument.Load(pathXml);
var comptes = doc.Descendants("Anzeige").Descendants("Kunde").Descendants("Konto").Select(p => new Compte()
{
NumCompte = p.Element("KtoNr") != null ? p.Element("KtoNr").Value : String.Empty,
typeCompte = p.Element("KontoArt") != null ? p.Element("KontoArt").Value : String.Empty,
Trans = getXmlTransactions(pathXml)
}).ToList();
return comptes;
}
How can I make a controf before adding items to the list. Thank you.
exemple of the xml file :
<Anzeige>
<Kunde>
<IdClient>ppp</IdClient>
<Konto>
<NumCompte>258</NumCompte>
<Transaction>
<idTrans>85555</idTrans>
<type>blebleble</type>
</Transaction>
<Transaction>
<idTrans>85555</idTrans>
<type>blebleble</type>
</Transaction>
</Konto>
</Kunde>
</Anzeige>
code of getXmlTransaction :
public static List<Transaction> getXmlTransactions(string pathXml)
{
var doc = XDocument.Load(pathXml);
var transactions = doc.Descendants("Anzeige").Descendants("Kunde").Descendants("Konto").Descendants("Transaktion").Select(p => new Transaction()
{
TransID = p.Element("TransID") != null ? p.Element("TransID").Value : String.Empty,
TypeTransaction = p.Element("TransArt") != null ? p.Element("TransArt").Value : String.Empty
}).ToList();
if (transactions != null)
return transactions.ToList();
else
return new List<Transaction>();
}

Use casting of element to string instead of reading it's value directly. If element was not found, you will have null string instead of exception:
var doc = XDocument.Load(pathXml);
var comptes = doc.Descendants("Anzeige")
.Descendants("Kunde")
.Descendants("Konto")
.Select(k => new Compte {
NumCompte = (string)k.Element("KtoNr"),
typeCompte = (string)k.Element("KontoArt"),
Trans = getXmlTransactions(k)
}).ToList();
If you want empty string instead of null when element not found, you can use null-coalescing operator
NumCompte = (string)p.Element("KtoNr") ?? ""
Use same technique for parsing nodes which may not exist. And I'm pretty sure that it's getXmlTransactions(pathXml) method throws exception.
UPDATE: Do not load whole xml document when you are getting transactions. How would you know which Konto element transactions to read. Pass Konto element instead and get its transactions:
public static List<Transaction> getXmlTransactions(XElement konto)
{
return konto.Elements("Transaction")
.Select(t => new Transaction {
TransID = (string)t.Element("idTrans"),
TypeTransaction = (string)t.Element("type")
}).ToList();
}
NOTE: You have <idTrans> (instead TransID) and <type> (instead TransArt) elements in <Transaction> (instead Transaktion)! Also there is no KtoNr and KontoArt elements in your xml. Read element names carefully. Also instead of looking for all descendants it's better to search in direct children:
doc.Root.Elements("Kunde").Elements("Konto") ...

Related

How to Read and Write to an XML file using XDocument and XElements? [duplicate]

I'm using LINQ together with XDocument to read a XML File. This is the code:
XDocument xml = XDocument.Load(filename);
var q = from b in xml.Descendants("product")
select new
{
name = b.Element("name").Value,
price = b.Element("price").Value,
extra = b.Element("extra1").Value,
deeplink = b.Element("deepLink").Value
};
Now the problem is, the extra1 field is not always present. There are items in the XML file without that node. If that happens it's crashing with a NullReferenceException.
Is there any possibility to include a "check if null" so I can prevent it from crashing?
Use (string) instead of .Value:
var q = from b in xml.Descendants("product")
select new
{
name = (string)b.Element("name"),
price = (double?)b.Element("price"),
extra = (string)b.Element("extra1"),
deeplink = (string)b.Element("deepLink")
};
This also works with other datatypes, including many nullable types in case the element is not always present.
You can use "null coalescing" operator:
var q = from b in xml.Descendants("product")
select new
{
name = (string)b.Element("name") ?? "Default Name",
price = (double?)b.Element("price") ?? 0.0,
extra = (string)b.Element("extra1") ?? String.Empty,
deeplink = (string)b.Element("deepLink") ?? String.Empty
};
This way, you will have full control about default value used when there's no element.
Use the following example for checking existence of any element before using that element.
if( b.Elements("extra1").Any() )
{
extra = b.Element("extra1").Value;
}
Here is sample example to read XML file using XDocument.
XDocument objBooksXML = XDocument.Load(Server.MapPath("books.xml"));
var objBooks = from book in
objBooksXML.Descendants("Book")
select new {
Title = book.Element("Title").Value,
Pages = book.Element("Pages").Value
};
Response.Write(String.Format("Total {0} books.", objBooks.Count()));
gvBooks.DataSource = objBooks;
gvBooks.DataBind();

Insert child element in the right order when working with OpenXML

I'm modifying .docx documents with DocumentFormat.OpenXml library. I know element ordering is important, otherwise the document will not pass schema validation and might result a document that can't be opened in Word.
Now I need to add a DocumentProtection element to DocumentSettingsPart. And I need to insert this child element in the right place inside of a parent.
The schema looks like this:
There are quite a lot of possible ordering of child elements. At the moment I'm adding this element like this:
var documentProtection = new DocumentProtection()
{
// do the configuration
};
DocumentSettingsPart settings = doc.MainDocumentPart.DocumentSettingsPart;
var rootElement = settings.RootElement;
var prevElement =
rootElement.GetFirstChild<DoNotTrackFormatting>() ??
rootElement.GetFirstChild<DoNotTrackMoves>() ??
rootElement.GetFirstChild<TrackRevisions>() ??
rootElement.GetFirstChild<RevisionView>() ??
rootElement.GetFirstChild<DocumentType>() ??
rootElement.GetFirstChild<StylePaneSortMethods>() ??
// SNIP
rootElement.GetFirstChild<Zoom>() ??
rootElement.GetFirstChild<View>() ??
(OpenXmlLeafElement)rootElement.GetFirstChild<WriteProtection>();
rootElement.InsertAfter(documentProtection, prevElement);
I.e. I'm trying to find if any possible element that should go before mine already exists in the document. And then insert DocumentProtection after that element. And given amount of elements this list gets pretty boring.
Is there a better way to add DocumentProtection so it is schema compliant and does not involve enumeration of all possible elements?
There isn't a nice way to achieve what you want. You'll have to tinker with the collection and you're responsible for keeping the order correct.
Using ILSpy on the Settings class you'll find that the implementors used a helper method SetElement<T> on the base class that takes a position and an instance to insert.
Unfortunately that helper method is marked internal so we can't leverage it if you try to subclass Settings. Instead I re-implemented the needed functionality so you'll have a subclass of Settings that does offer a property for DocumentProtection but uses the re-implemented solution to find the correct location to insert the node:
SettingsExt
public class SettingsExt: Settings
{
// contruct based on XML
public SettingsExt(string outerXml)
:base(outerXml)
{
// empty
}
public DocumentProtection DocumentProtection
{
// get is easy
get
{
return this.GetFirstChild<DocumentProtection>();
}
// reimplemented SetElement based on
// reversed engineered Settings class
set
{
// eleTagNames is a static string[] declared later
// it holds all the names of the elements in the right order
int sequenceNumber = eleTagNames
.Select((s, i) => new { s= s, idx = i })
.Where(s => s.s == "documentProtection")
.Select((s) => s.idx)
.First();
OpenXmlElement openXmlElement = this.FirstChild;
OpenXmlElement refChild = null;
while (openXmlElement != null)
{
// a bit naive
int currentSequence = eleTagNames
.Select((s, i) => new { s = s, idx = i })
.Where(s => s.s == openXmlElement.LocalName)
.Select((s) => s.idx)
.First(); ;
if (currentSequence == sequenceNumber)
{
if (openXmlElement is DocumentProtection)
{
refChild = openXmlElement.PreviousSibling();
this.RemoveChild<OpenXmlElement>(openXmlElement);
break;
}
refChild = openXmlElement;
}
else
{
if (currentSequence > sequenceNumber)
{
break;
}
refChild = openXmlElement;
}
openXmlElement = openXmlElement.NextSibling();
}
if (value != null)
{
this.InsertAfter(value, refChild);
}
}
}
// order of elements in the sequence!
static readonly string[] eleTagNames = new string[]
{
"writeProtection",
"view",
"zoom",
"removePersonalInformation",
"removeDateAndTime",
"doNotDisplayPageBoundaries",
"displayBackgroundShape",
"printPostScriptOverText",
"printFractionalCharacterWidth",
"printFormsData",
"embedTrueTypeFonts",
"embedSystemFonts",
"saveSubsetFonts",
"saveFormsData",
"mirrorMargins",
"alignBordersAndEdges",
"bordersDoNotSurroundHeader",
"bordersDoNotSurroundFooter",
"gutterAtTop",
"hideSpellingErrors",
"hideGrammaticalErrors",
"activeWritingStyle",
"proofState",
"formsDesign",
"attachedTemplate",
"linkStyles",
"stylePaneFormatFilter",
"stylePaneSortMethod",
"documentType",
"mailMerge",
"revisionView",
"trackRevisions",
"doNotTrackMoves",
"doNotTrackFormatting",
"documentProtection",
"autoFormatOverride",
"styleLockTheme",
"styleLockQFSet",
"defaultTabStop",
"autoHyphenation",
"consecutiveHyphenLimit",
"hyphenationZone",
"doNotHyphenateCaps",
"showEnvelope",
"summaryLength",
"clickAndTypeStyle",
"defaultTableStyle",
"evenAndOddHeaders",
"bookFoldRevPrinting",
"bookFoldPrinting",
"bookFoldPrintingSheets",
"drawingGridHorizontalSpacing",
"drawingGridVerticalSpacing",
"displayHorizontalDrawingGridEvery",
"displayVerticalDrawingGridEvery",
"doNotUseMarginsForDrawingGridOrigin",
"drawingGridHorizontalOrigin",
"drawingGridVerticalOrigin",
"doNotShadeFormData",
"noPunctuationKerning",
"characterSpacingControl",
"printTwoOnOne",
"strictFirstAndLastChars",
"noLineBreaksAfter",
"noLineBreaksBefore",
"savePreviewPicture",
"doNotValidateAgainstSchema",
"saveInvalidXml",
"ignoreMixedContent",
"alwaysShowPlaceholderText",
"doNotDemarcateInvalidXml",
"saveXmlDataOnly",
"useXSLTWhenSaving",
"saveThroughXslt",
"showXMLTags",
"alwaysMergeEmptyNamespace",
"updateFields",
"hdrShapeDefaults",
"footnotePr",
"endnotePr",
"compat",
"docVars",
"rsids",
"mathPr",
"uiCompat97To2003",
"attachedSchema",
"themeFontLang",
"clrSchemeMapping",
"doNotIncludeSubdocsInStats",
"doNotAutoCompressPictures",
"forceUpgrade",
"captions",
"readModeInkLockDown",
"smartTagType",
"schemaLibrary",
"shapeDefaults",
"doNotEmbedSmartTags",
"decimalSymbol",
"listSeparator",
"docId",
"discardImageEditingData",
"defaultImageDpi",
"conflictMode"};
}
A typical usage scenario with this class is as follows:
using (var doc = WordprocessingDocument.Open(#"c:\tmp\test.docx", true))
{
var documentProtection = new DocumentProtection()
{
Formatting = DocumentFormat.OpenXml.OnOffValue.FromBoolean(true)
};
DocumentSettingsPart settings = doc.MainDocumentPart.DocumentSettingsPart;
// instantiate our ExtendedSettings class based on the
// original Settings
var extset = new SettingsExt(settings.Settings.OuterXml);
// new or existing?
if (extset.DocumentProtection == null)
{
extset.DocumentProtection = documentProtection;
}
else
{
// replace existing values
}
// this is key to make sure our own DOMTree is saved!
// don't forget this
settings.Settings = extset;
}

How do I nest this LINQ query?

I have an XML document from a web service that I am trying to query. However, I am not sure how to query the XML when it has elements nested inside other elements.
Here is a section of the XML file (I haven't included all of it because it's a long file):
<response>
<display_location>
<full>London, United Kingdom</full>
<city>London</city>
<state/>
<state_name>United Kingdom</state_name>
<country>UK</country>
<country_iso3166>GB</country_iso3166>
<zip>00000</zip>
<magic>553</magic>
<wmo>03772</wmo>
<latitude>51.47999954</latitude>
<longitude>-0.44999999</longitude>
<elevation>24.00000000</elevation>
</display_location>
<observation_location>
<full>London,</full>
<city>London</city>
<state/>
<country>UK</country>
<country_iso3166>GB</country_iso3166>
<latitude>51.47750092</latitude>
<longitude>-0.46138901</longitude>
<elevation>79 ft</elevation>
</observation_location>
I can query "one section at a time" but I'm constructing an object from the LINQ. For example:
var data = from i in weatherResponse.Descendants("display_location")
select new Forecast
{
DisplayFullName = i.Element("full").Value
};
var data = from i in weatherResponse.Descendants("observation_location")
select new Forecast
{
ObservationFullName = i.Element("full").Value
};
And my "Forecast" class is basically just full of properties like this:
class Forecast
{
public string DisplayFullName { get; set; };
public string ObservationFullName { get; set; };
//Lots of other properties that will be set from the XML
}
However, I need to "combine" all of the LINQ together so that I can set all the properties of the object. I have read about nested LINQ but I do not know how to apply it to this particular case.
Question: How do I go about "nesting/combining" the LINQ so that I can read the XML and then set the appropriate properties with said XML?
One possible way :
var data = from i in weatherResponse.Descendants("response")
select new Forecast
{
DisplayFullName = (string)i.Element("display_location").Element("full"),
ObservationFullName = (string)i.Element("observation_location").Element("full")
};
Another way ... I prefer using the Linq extension methods in fluent style
var results = weatherResponse.Descendants()
.SelectMany(d => d.Elements())
.Where(e => e.Name == "display_location" || e.Name == "observation_location")
.Select(e =>
{
if(e.Name == "display_location")
{
return new ForeCast{ DisplayFullName = e.Element("full").Value };
}
else if(e.Name == "observation_location")
{
return new ForeCast{ ObservationFullName = e.Element("full").Value };
}
else
{
return null;
}
});

NullReferenceException was unhandled error when trying to retrieve config attributes

public string GetLogName(string config)
{
XDocument xDoc = XDocument.Load(config);
XElement[] elements = xDoc.Descendants("listeners").Descendants("add").ToArray();
foreach (var element in elements)
{
if (element.Attribute("fileName").Value != null)
{
string filename = element.Attribute("fileName").Value;
int location = filename.IndexOf("%");
Console.WriteLine("string to return: " + filename.Substring(0, location));
return filename.Substring(0, location);
}
}
}
I am trying to retrieve the "fileName" attribute from each element in the elements array, but there are some cases when the "fileName" attribute does not exist and fails with the following error: NullReferenceException was unhandled. Object reference not set to an instance of an object.
In my case there are two "add" nodes that do not have a "fileName" attribute, but the third add node has it.
How can I skip over entries that do not have a "fileName" attribute, or can you recommend a better way to retrieve this attribute?
One way is to filter out the list before you process it:
XElement[] elements = xDoc.Descendants("listeners")
.Descendants("add")
.Where (d => d.Attribute("filename") != null )
.ToArray();
--- IMHO this is how I would rewrite the method, using linq and regex ---
var elements =
XDocument.Load(config);
.Descendants("listeners")
.Descendants("add")
.Where (node => node.Attribute("filename") != null )
.ToList();
return elements.Any() ? elements.Select (node => node.Attribute("filename").Value )
.Select (attrValue => Regex.Match(attrValue, "([^%]+)").Groups[1].Value)
.First ()
: string.Empty;
You should be able to do this simply by changing this line:
if (element.Attribute("fileName").Value != null)
To:
if (element.Attribute("fileName") != null)
change your if statement to this :
if (element.Attribute("fileName") != null)

wrong value in result query so Null Reference Exception in Linq query

I have two xml files which I am comparing with each other. The Linq query result1 is throwing Null Reference Exception after executing correctly for one rule. And when I debugged I found the section is displaying wrong values. I am unable to figure out the cause.
Rules.xml file:
<rule id="1" numberofsections="2">
<section id="1" attributeid="1686" ruleoperator="==" condition="and">
<name>Processor type</name>
<value>Core i3</value>
</section>
<section id="2" attributeid="1438" ruleoperator="<" condition="and" >
<name>Weight</name>
<value>3.8 LBS</value>
</section>
<type>ultrabook</type>
</rule>
And the code snippet:
XDocument rulesXml = XDocument.Load("/RulesEnginescope/RulesEnginescope/rulesSubType.xml");
XDocument productXml = XDocument.Load("c:/RuleEngine/RuleEngine/product.xml");
var getSelectedLeafCategoryRules = from rules2 in rulesXml.Descendants("QueryTransformation").Descendants("leafcategory")
where ((long)System.Convert.ToDouble(rules2.FirstAttribute.Value) == 4590)
select rules2;
var rules = getSelectedLeafCategoryRules.Descendants("rule");
var productAttribute = productXml.Descendants("AttrList").Descendants("Attr");
foreach (var x in rules)
{
var section = x.Elements("section");
/*Wrong value in section.count()*/
Console.WriteLine(section.Count());
var result1 = from p in section
from pa in productAttribute
where (p.Attribute("attributeid").Value == pa.Attribute("id").Value
&& p.Element("name").Value == pa.Element("Name").Value)
select new
{
ruleAttribute = new
{
ruleId = p.Attribute("attributeid").Value,
ruleOperator = p.Attribute("ruleoperator").Value,
name = p.Element("name").Value,
value = p.Element("value").Value,
condition = p.Attribute("condition").Value
},
prodAttribute = new
{
productId = pa.Attribute("id").Value,
name = pa.Element("Name").Value,
value = pa.Element("ValueList").Element("Value").Value
/*Error*/ }
};
if (result1.Count() != 0 && result1.Count() == System.Convert.ToInt64(x.Attribute("numberofsections").Value))
{
//checking each section
foreach (var r in result1)
{
...
}
}
The idiomatic way to get the value of elements and attributes in LINQ-to-XML is to cast the element or attribute to the type you want, rather than accessing the Value attribute.
prodAttribute = new
{
productId = (string)pa.Attribute("id"),
name = (string)pa.Element("Name"),
// ...
}
Using this pattern avoids null ref exceptions caused when calls to Attribute() and Element() don't find a matching node. It also reduces verbosity:
((long)System.Convert.ToDouble(rules2.FirstAttribute.Value)
// should be
(long)rules2.FirstAttribute
You'll still need to add null checks when you're accessing children of children. This can get verbose; one way to keep it succinct is to use IEnumerable-oriented methods so that you're operating on a (possibly empty) collection, rather than a (possibly null) instance.
pa.Element("ValueList").Element("Value").Value
// could be
(string)pa.Elements("ValueList").Elements("Value").FirstOrDefault ()
Finally, note that capitalization matters in LINQ-to-XML. In your code you seem to be switching capitalization patterns ("id" vs. "Name") often; it's likely that your source XML is more consistent.

Categories