Share via

access element in an XML document

lawrence 136 Reputation points
2021-06-24T23:19:45.87+00:00

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 | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


3 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,611 Reputation points
    2021-06-28T08:56:35.737+00:00

    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.

    Was this answer helpful?


  2. Yitzhak Khabinsky 27,196 Reputation points
    2021-06-25T12:51:48.503+00:00

    Hi @lawrence ,

    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'  
    

    Was this answer helpful?


  3. lawrence 136 Reputation points
    2021-06-25T07:34:26.717+00:00

    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

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.