Reader Level:
ARTICLE

How to Read / Search Node or Attribute in XML file

Posted by Gohil Jayendrasinh Articles | XML December 21, 2010
In this article you will learn how to Read / Search Node or Attribute in XML file.
  • 0
  • 0
  • 20899

My XML file is As Below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<Fields>

  <Field ID="1" Datatype="Option" Enabled="1" FieldClass="Normal" Name="Document Type" CaptionML="ENU=Document Type" OptionString="Quote,Order,Invoice,Credit Memo,Blanket Order,Return Order" OptionCaptionML="ENU=Quote,Order,Invoice,Credit Memo,Blanket Order,Return Order" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None"/>

  <Field ID="2" Datatype="Code" DataLength="20" Enabled="1" FieldClass="Normal" Name="Sell-to Customer No." CaptionML="ENU=Sell-to Customer No." BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="0" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None">

    <TableRelations TableID="18" TableName="Customer" FieldID="0"/>

  </Field>

  <Field ID="3" Datatype="Code" DataLength="20" Enabled="1" FieldClass="Normal" Name="Document No." CaptionML="ENU=Document No." BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None">

    <TableRelations TableID="36" TableName="Sales Header" FieldID="3">

      <Filters FilterGroup="0" FieldID="1" FilterType="FIELD" FilterValue="1"/>

    </TableRelations>

  </Field>

  <Field ID="4" Datatype="Integer" Enabled="1" FieldClass="Normal" Name="Line No." CaptionML="ENU=Line No." BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None"/>

  <Field ID="5" Datatype="Option" Enabled="1" FieldClass="Normal" Name="Type" CaptionML="ENU=Type" OptionString=" ,G/L Account,Item,Resource,Fixed Asset,Charge (Item)" OptionCaptionML="ENU=&quot; ,G/L Account,Item,Resource,Fixed Asset,Charge (Item)&quot;" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" OnValidate="134" ExtendedDatatype="None"/>

  <Field ID="6" Datatype="Code" DataLength="20" Enabled="1" FieldClass="Normal" Name="No." CaptionML="ENU=No." BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" OnValidate="135" ExtendedDatatype="None">
    <TableRelations TableID="7" TableName="Standard Text" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="0"/>

    </TableRelations>

    <TableRelations TableID="15" TableName="G/L Account" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="1"/>

    </TableRelations>

    <TableRelations TableID="27" TableName="Item" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="2"/>

    </TableRelations>

    <TableRelations TableID="156" TableName="Resource" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="3"/>

    </TableRelations>

    <TableRelations TableID="5600" TableName="Fixed Asset" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="4"/>

    </TableRelations>

    <TableRelations TableID="5800" TableName="Item Charge" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="5"/>

    </TableRelations>

  </Field>

  <Field ID="5800" Datatype="Boolean" Enabled="1" FieldClass="Normal" Name="Allow Item Charge Assignment" CaptionML="ENU=Allow Item Charge Assignment" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" InitValue="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" OnValidate="206" ExtendedDatatype="None"/>

  <Field ID="5801" Datatype="Decimal" Enabled="1" FieldClass="FlowField" Name="Qty. to Assign" CaptionML="ENU=Qty. to Assign" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="0" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None">

    <CalculationFormula FlowType="Sum" ReverseSign="0" Table="5809" Field="8">

      <TableFilters>

        <Filter FilterGroup="0" FieldID="1" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="1"/>

        <Filter FilterGroup="0" FieldID="2" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="3"/>

        <Filter FilterGroup="0" FieldID="3" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="4"/>

      </TableFilters>

    </CalculationFormula>

  </Field>

  <Field ID="5802" Datatype="Decimal" Enabled="1" FieldClass="FlowField" Name="Qty. Assigned" CaptionML="ENU=Qty. Assigned" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="0" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None">

    <CalculationFormula FlowType="Sum" ReverseSign="0" Table="5809" Field="9">

      <TableFilters>

        <Filter FilterGroup="0" FieldID="1" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="1"/>

        <Filter FilterGroup="0" FieldID="2" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="3"/>

        <Filter FilterGroup="0" FieldID="3" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="4"/>

      </TableFilters>

    </CalculationFormula>

  </Field>

</Fields>

 

 Searching Code

 /// <summary>

        ///

        /// </summary>

        /// <param name="XMLPath">File Path</param>

        /// <param name="SearchNode">Search Node</param>

        /// <param name="AttributeName">Attribute Node</param>

        /// <param name="AttributeValue">Attribute Value</param>

        /// <param name="Connectionstring">Database ConnectionString</param>

        private void InsertintoTableRelationInfo(string XMLPath, string SearchNode, string AttributeName, string AttributeValue, string Connectionstring)

        {

            #region Table RelationInfo

            XmlDocument doc = new XmlDocument();

            doc.Load(XMLPath);

 

            System.Text.StringBuilder str = new StringBuilder();

 

            XmlNodeList nodes = doc.GetElementsByTagName(SearchNode);

 

            foreach (XmlNode node in nodes)

            {

 

                if (node.HasChildNodes)

                {

                    foreach (XmlAttribute attribute in node.Attributes)

                    {

                        if (attribute.Name == AttributeName)

                        {

                            if (attribute.Value == AttributeValue)

                            {

 

                                for (int i = 0; i < node.ChildNodes.Count; i++)

                                {

                                    string name = node.ChildNodes[i].Name;

                                    /*

                                     * Check ChildNode

                                     */

                                    if (name == "TableRelations")

                                    {

                                        foreach (XmlAttribute chieldAttribute in node.ChildNodes[i].Attributes)

                                        {

                                            /*

                                             * Check ChildNode Attribute

                                             */

                                            if (chieldAttribute.Name == "TableID")

                                            {

                                                _TableRelationsTableID = Convert.ToInt32(chieldAttribute.Value);

 

                                            }

                                            if (chieldAttribute.Name == "TableName")

                                            {

                                                _TableName = chieldAttribute.Value;

                                            }

                                            if (chieldAttribute.Name == "FieldID")

                                            {

                                                _TableFieldID = Convert.ToInt32(chieldAttribute.Value);

                                            }                                           

                                        }

                                        _CalculationFormulaFlowType = "TableRelation";

                                        using (dbNavXmlDataContext db = new dbNavXmlDataContext(Connectionstring))

                                        {

                                            db.USP_INSERT_TableRelationInfo(_ObjectType, _ObjectID, _ID, Convert.ToInt32(AttributeValue), _TableRelationsTableID, _TableName, _TableFieldID, _CalculationFormulaFlowType, _ReverseSign);

                                        }

                                    }

                                    if (name == "CalculationFormula")

                                    {

                                        foreach (XmlAttribute chieldAttribute in node.ChildNodes[i].Attributes)

                                        {

                                            if (chieldAttribute.Name == "FlowType")

                                            {

                                                _CalculationFormulaFlowType = chieldAttribute.Value;

 

                                            }

                                            if (chieldAttribute.Name == "ReverseSign")

                                            {

                                                _ReverseSign = Convert.ToInt32(chieldAttribute.Value);

                                            }

                                          

                                        }

                                        

                                        using (dbNavXmlDataContext db = new dbNavXmlDataContext(Connectionstring))

                                        {

                                            db.USP_INSERT_TableRelationInfo(_ObjectType, _ObjectID, _ID, Convert.ToInt32(AttributeValue), _TableRelationsTableID, _TableName, _TableFieldID, _CalculationFormulaFlowType, _ReverseSign);

                                        }

                                    }

                                }

                            }
                       }

                    }

                 }
 
            #endregion
            }
 
        }

COMMENT USING

Trending up