Question

I am developing silverlight web part for sharepoint 2010. I have an xml file in my application as follows

<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="67" uniqueCount="39">
  <si>
    <t>INVOICE</t>
  </si>
  <si>
    <t>INVOICE #</t>
  </si>
  <si>
    <t>Bill To:</t>
  </si>
  <si>
    < t>DESCRIPTION</t>
  </si>
  <si>
    <t>AMOUNT</t>
  </si>
  <si>
    <t>TOTAL</t>
  </si>
  <si>
    <t>FOR:</t>
  </si>
  <si>
    <t>Positive Behavior Support Corp</t>
  </si>
  <si>
    <t>8108 SE Coconut St.</t>
  </si>
  <si>
    <t>Hobe Sound, FL 33455</t>
  </si>
  <si>
    <t>772-349-6317 Phone  772-675-9100 Fax</t>
  </si>
  <si>
    <t>EIN 20-5268843</t>
  </si>
  <si>
    <t>Provider 693068996</t>
  </si>
  <si>
    <t>Rate</t>
  </si>
  <si>
    <t>Units</t>
  </si>
  <si>
    <t>DATE</t>
  </si>
  <si>
    <t>Michael Nolan Ph.D. BCBA</t>
  </si>
  <si>
    <t>____________________________________________</t>
  </si>
  <si>
    <t>BCBA                          Date</t>
  </si>
  <si>
    <t>CLIENT:</t>
  </si>
  <si>
    <t>Date:</t>
  </si>
  <si>
    <t>Behavior Assistant- L. Bresson</t>
  </si>
  <si>
    <t>Email:</t>
  </si>
  <si>
    <t>1 Unit = 1 hour</t>
  </si>
  <si>
    <t>TOTALS</t>
  </si>
  <si>
    <t>cvt1970@juno.com</t>
  </si>
  <si>
    <t>Attn: Cecilia</t>
  </si>
  <si>
    <t>Behavior Assistant- B. Bresson</t>
  </si>
  <si>
    <t>Behaviror Ass't -N Giarratano</t>
  </si>
  <si>
    <t>Signature of Representatives Approval           Date</t>
  </si>
  <si>
    <r>
      <t xml:space="preserve">Behavior Asst- </t>
    </r>
    <r>
      <rPr>
        <sz val="9" />
        <rFont val="Arial" />
        <family val="2" />
      </rPr>
      <t>N Giarratano-</t>
    </r>
    <r>
      <rPr>
        <sz val="10" />
        <rFont val="Arial" />
        <family val="2" />
      </rPr>
      <t>08</t>
    </r>
  </si>
  <si>
    <t>Behavior Asst- B. Bresson-08</t>
  </si>
  <si>
    <t>Behavior Asst- L. Bresson-08</t>
  </si>
  <si>
    <t>&lt;@Invoice&gt;</t>
  </si>
  <si>
    <t>&lt;@For&gt;</t>
  </si>
  <si>
    <t>&lt;@Client&gt;</t>
  </si>
  <si>
    <t>&lt;@Caregiver&gt;</t>
  </si>
  <si>
    <t>&lt;@Email,@Address,@City,@State,@Zip&gt;</t>
  </si>
  <si>
    <t>&lt;@Date&gt;</t>
  </si>
</sst>

I am successfully loading this xml file. Now I am acessing all the element with name "t" from xml file as follows.

XNamespace ns = xmlDoc.Root.Name.Namespace;
            var eles = from c in xmlDoc.Descendants(ns + "si")
                       select c.Element(ns + "t");

It is also working fine. But when I use the following query it gives me error

eles.Elements(ns + "t").Where(x => x.Element(ns + "t").Value == "&lt;@Date&gt;").SingleOrDefault().SetElementValue(ns + "t","hi");

It gives me error as object reference is not set to instance of object. How I can replace the "<@Date> with "hi" in the existing xml. Can you please provide me any code or link through which I can resolve the above issue ?

Was it helpful?

Solution

Your eles variable is already returning the <t>. None of the <t> have a child that is <t>, so it makes sense that x.Element(ns + "t") doesn't return anything (thus null, thus .Value is an error). Actually, I'm a bit confused, because eles.Elements(ns + "t") should have been an empty sequence.

Also, the value in C# terms is <@Date>; the &gt; / &lt; only applies to xml.

Also, the query is being impacted by the one <si> that doesn't have a <t>; a better variant would be:

var eles = xmlDoc.Descendants(ns + "si").Elements(ns + "t");

(which handles 0, 1, or many <t> per <si>)

Then after that, you mean:

eles.SingleOrDefault(x => x.Value == "<@Date>").Value = "hi";

OTHER TIPS

Your first query returns elements with the name 't', your second query then looks for children of these elements, also with the name 't'. This does not match the structure of your document, Your second query should be:

var matchingElement = eles.SingleOrDefault(x => x.Value == "&lt;@Date&gt;");

You can then perform your replacement.

I would recommend using your debugger when developing code. If you breakpoint then inspect the eles variable you can actually see what is returned by your first query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top