question

abiodunajilawrence avatar image
0 Votes"
abiodunajilawrence asked TimonYang-MSFT commented

access element in an XML document

I will appreciate any urgent help please on how to access Element from an XLM document shown below to be used in the values tag.

My XML document generates both the attribute vs element as shown below.

My Problem is I don't know the right dataPath to access its element

   <PremiumComputation>\r\n    
     <RecordID>3</RecordID>\r\n    
     <TotalSumInsured>1136000000.00</TotalSumInsured>\r\n    
 </PremiumComputation>

<values>
<value name="TotalSumInsured" dataPath="PolicyData/@TotalSumInsured" binding="Static" required="true"/>
</values>


the above script gives error message :
Mapping XML user-defined value data attribute not found. value=TotalSumInsured, data=PremiumComputation/@TotalSumInsured

Again i tried
dataPath="TotalSumInsured"
dataPath="PremiumComputation/TotalSumInsured"

both gave the error message:
Object reference not set to an instance of an object.

What is the right way to access the path please.

Best regards

Lawrence

sql-server-generaldotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@abiodunajilawrence,

It is not clear what you are asking.
Please revise your question, and provide a minimal reproducible example.

1 Vote 1 ·

The second dataPath seems appropriate. Which line or component gives "Object reference not set to an instance of an object"?


0 Votes 0 ·
abiodunajilawrence avatar image
0 Votes"
abiodunajilawrence answered abiodunajilawrence commented

Thank you for your prompt response YitzhakKhabinsky.

Here is my XML document root below:

 <document dataPath="document/PolicySchedulePrintDTO">
    
 <values>
             <value name="TotalSuminsured" 
             dataPath="PremiumComputation/@TotalSumInsured" binding="Static" required="true"/>
 </values>
 <predicates>
       <predicate name="IsTotalSuminsuredZero">
             <match when="TotalSuminsured" condition="gt" value="0"/>
        </predicate>
 </predicates>
    
 <generate layout="PolicySchedule"/>
    
 </document>


Premium Computation Section


 <body>
 <section id="computation" dataPath="PremiumComputation">
      <table id="computationInformationTable">
    
  <columns>
       <column width="300"/>
        <column width="200"/>
          <column/>
  </columns>
        
  <rows>
   <row predicate="IsTotalSumInsuredZero" result="true">
    <cell leftBorder="yes" bottomBorder="yes" topBorder="yes" font="ThemeBold" rightBorder="yes" >
             <text align="Right"  fontSize="9" height="12">Total Sum Insured</text> 
      </cell>
       <cell  rightBorder="yes" topBorder="yes">
      <text dataPath="TotalSumInsured" font="ThemeBold" align="Left" type="Single" format="{0:#,##0.00}" height="8"/>
      </cell>
      </row>
  </rows>
 </table>
 </section>
 </body>


The XML serialized result is stated below. I want to access the <TotalSumInsured> from the XML Serialized data shown below.

XML Serialized result <PremiumComputation>\r\nRecordID>3</RecordID>\r\n<TotalSumInsured>1136000000.00</TotalSumInsure>\r\n </PremiumComputation>


looking at the serialized XML result above, it has both Attributes and elements.

How do I access the element <TotalSumInsured> from the XML Serialized result above.
I need TotalSumInsured to test for the content in the <Values> tag above and use the result in the <Predicates> tag to hide a row information on the PDF document under Premium Computation script above, when the TotalSuminsured condition is not greater than 0 as shown in the script section under <predicate>

My main problem is I don't how to access the path where the<TotalSumInsured> is located in the XML . Instead of generating the PDF, the system throws an error message below:

"Mapping XML user-defined value data attribute not found. value=TotalSumInsured, data=PremiumComputation/@TotalSumInsured"

Please what am I doing wrong in the path below:

dataPath="PremiumComputation/@TotalSumInsured" binding="Static" required="true"/>

I hope it is clear now.

Best regards,

Lawrence











· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is still quite confusing. I see that you have tagged this with both sql-server-general and dotnet-csharp. I'm reading this from the SQL Server side, but it is not clear to me if you are trying extract the values in T-SQL or in C#. So I think could a start could be if you could tell us if which language you want help with.

0 Votes 0 ·

Thank you ErlandSommarskog.
I am so sorry I did not mentioned the Language .

I am looking for help on C# please.

I want to generate report from a Model into PDF but first pass it through the XML to serialized the data and filter off rows that did not match a set values condition at the document root in the XML document.

My problem is how to access the XML document path to get the TotalSumInsured to be used to set the filter condition.

dataPath="PremiumComputation/@TotalSumInsured" binding="Static" required="true"/>

"Mapping XML user-defined value data attribute not found. value=TotalSumInsured, data=PremiumComputation/@TotalSumInsured"

Thank you in anticipation of your quick response.

Best regards

Lawrence

0 Votes 0 ·
YitzhakKhabinsky-0887 avatar image
0 Votes"
YitzhakKhabinsky-0887 answered abiodunajilawrence commented

Hi @abiodunajilawrence,

Please try the following solution.

c#

 void Main()
 {
     XDocument xdoc = XDocument.Parse(@"<PremiumComputation>
        <RecordID>3</RecordID>
        <TotalSumInsured>1136000000.00</TotalSumInsured>
     </PremiumComputation>");
    
     string TotalSumInsured = xdoc.Descendants("TotalSumInsured").FirstOrDefault().Value;
    
     Console.WriteLine("TotalSumInsured: '{0}'", TotalSumInsured);
 }

Output

 TotalSumInsured: '1136000000.00'
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Thank you YitzhakKhabinsky.

The difference here is that we are working backward..."Code to XML".
while your solution example above is "XML to Code"

How can I adapt your solution code to "Code to XML" please?

Thank you so much.

Best regards.

Lawrence

0 Votes 0 ·
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

Do you mean to write something from code to xml?

Try something like this:

 string filePath = @"C:\xxx\1.xml";
             string xmlString = File.ReadAllText(filePath);
             XDocument xdoc = XDocument.Parse(@xmlString);
    
             var TotalSumInsured = xdoc.Descendants("values");
             foreach (var item in TotalSumInsured)
             {
                 foreach (var node in item.Nodes())
                 {
                     if (node.NodeType == XmlNodeType.Element)
                     {
                         Console.WriteLine(((XElement)node).Attribute("dataPath").Value);
                         ((XElement)node).SetAttributeValue("dataPath", "xxx");
                         // or
                         ((XElement)node).Attribute("dataPath").Value = "jjj";
                     } 
                 }
             }
             xdoc.Save(filePath);

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@abiodunajilawrence
May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·