1

I have a linq syntax to get elemnts from XML, this is the XML syntax:

<bbb ccc="12345">
  <productA>FIRST</productA>
  <!--productB>SECOND</productB-->
  <productC>THIRD</productC>
</bbb>

I want to init a string array with the products :

string[] aaa =
                 (from el in keyValueList.Elements("bbb")
                  where (string)el.Attribute("ccc") == "12345"
                  select new string[] { (el.Attribute("productA").Value), el.Attribute("productB").Value, el.Attribute("productC").Value }).FirstOrDefault();

Now I try save space when the product not found (like productB), I can not use DefaultIfEmpty() but it is not compiling... like:

   string[] aaa =
                 (from el in keyValueList.Elements("bbb")
                  where (string)el.Attribute("ccc") == "12345"
                  select new string[] { (el.Attribute("productA").Value).DefaultIfEmpty(string.Empty), el.Attribute("productB").Value.DefaultIfEmpty(string.Empty), el.Attribute("productC").Value.DefaultIfEmpty(string.Empty) }).FirstOrDefault();

In conclusion, I want to get this array: {"FIRST",null,"THIRD"} and not {"FIRST","THIRD"}

Can I do it?

user1012506
  • 2,048
  • 1
  • 26
  • 45
  • What types does `DefaultIfEmpty` accept? What type is ...`Value`? Why are you passing `String.Empty` if you want `null`? Why do you think having `null` in the array saves space? How many items do you have that you think you need to save space? – NetMage Apr 11 '18 at 19:27
  • 1
    The null item is a comment and not an element. Why is the item commented out? – jdweng Apr 12 '18 at 04:59
  • If the null item was not in comment, was not a problem! I want to Know to match a specific product to the to the suitable value from aaa string, And I try do do it by index (aaa[2] have to contain "THIRD" value (productC ) etc), if tha aaa= {"FIRST","THIRD"} so aaa[2] was out of range or null), yes? – user1012506 Apr 12 '18 at 07:40

0 Answers0