Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading xml file with xpath

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
0
0
IT
hallo I have an Xml file like this:

Code:
<?xml version="1.0"?>
<HomeFinanceXMLBase>
  <Content>
    <Configuration>
      <Data>
        <UserDefined>
          <CostTypeList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.CostType, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" />
          <CostCategoryList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.CostCategory, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" />
          <PaymentModeList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.PaymentMode, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" />
        </UserDefined>
        <Default>
          <CostTypeList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.CostType, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]">
            <CostType Type="ch.davico.HomeFinance.Model.CostType">
              <Identity Type="System.Guid">A9936A85-D892-4F28-B4C9-11512F9CACA0</Identity>
              <Name Type="System.String">Fix</Name>
            </CostType>
          </CostTypeList>
          <CostCategoryList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.CostCategory, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]">
            <CostCategory Type="ch.davico.HomeFinance.Model.CostCategory">
              <Identity Type="System.Guid"></Identity>
              <Name Type="System.String">Tax</Name>
            </CostCategory>
          </CostCategoryList>
          <PaymentModeList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.PaymentMode, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]">
            <PaymentMode Type="ch.davico.HomeFinance.Model.PaymentMode">
              <Identity Type="System.Guid">13EA6A12-C13F-41CB-924E-BDD64416D669</Identity>
              <Name Type="System.String">e-Banking</Name>        
            </PaymentMode>     
          </PaymentModeList>
        </Default>
      </Data>
    </Configuration>
    <Base>
      <ServiceList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.Service, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" />
      <InvoiceList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.Invoice, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" />
      <CostList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.Cost, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" />
      <PaymentList Type="System.Collections.Generic.List`1[[ch.davico.HomeFinance.Model.Payment, ch.davico.HomeFinance.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" />
    </Base>
  </Content>
</HomeFinanceXMLBase>

Under "configuration" and then "data" there are two children "UserDefined" and "Default". Within these two tags there are 3 lists "CostTypeLists", "CostCategoryList", "PaymentModeList".

By using xPath I can read the first set of lists (under "UserDefined": now are empty for clearness), but I wont be able to read the second set of lists under "Default".

I don't know why.

Here is the code I use for reading:

Code:
        public void ReadXml(XmlReader reader)
        {
            ReadConfigurationData(reader);
            ReadBaseData(reader);
        }

        private void ReadConfigurationData(XmlReader reader)
        {
            //read user defined data
            ReadList(reader, "//HomeFinanceXMLBase/Content/Configuration/Data/UserDefined/CostTypeList/CostType", typeof(CostType));
            ReadList(reader, "//HomeFinanceXMLBase/Content/Configuration/Data/UserDefined/CostCategoryList/CostCategory", typeof(CostCategory));
            ReadList(reader, "//HomeFinanceXMLBase/Content/Configuration/Data/UserDefined/PaymentModeList/PaymentMode", typeof(PaymentMode));
            //read default data
            ReadList(reader, "//HomeFinanceXMLBase/Content/Configuration/Data/Default/CostTypeList/CostType", typeof(CostType));
            ReadList(reader, "//HomeFinanceXMLBase/Content/Configuration/Data/Default/CostCategoryList/CostCategory", typeof(CostCategory));
            ReadList(reader, "//HomeFinanceXMLBase/Content/Configuration/Data/Default/PaymentModeList/PaymentMode", typeof(PaymentMode));
        }

        private void ReadList(XmlReader reader, string xpath, Type type)
        {
            XPathDocument xPath = new XPathDocument(reader);
            XPathNavigator navigator = xPath.CreateNavigator();
            XPathNodeIterator iterator = navigator.Select(xpath);

            while (iterator.MoveNext())
            {
                object o = Activator.CreateInstance(type);

                switch (type.Name)
                {
                    case "CostType":
                        o = ReadCostType(iterator.Current);
                        this.CostTypeList.Add((CostType)o);
                        break;
                    case "CostCategory":
                        o = ReadCostCategory(iterator.Current);
                        this.CostCategoryList.Add((CostCategory)o);
                        break;
                    case "PaymentMode":
                        o = ReadPaymentMode(iterator.Current);
                        this.PaymentModeList.Add((PaymentMode)o);
                        break;
                    case "Cost":
                        o = ReadCost(iterator.Current);
                        this.CostList.Add((Cost)o);
                        break;
                    case "Invoice":
                        o = ReadInvoice(iterator.Current);
                        this.InvoiceList.Add((Invoice)o);
                        break;
                    case "Payment":
                        o = ReadPayment(iterator.Current);
                        this.PaymentList.Add((Payment)o);
                        break;
                    case "Service":
                        o = ReadService(iterator.Current);
                        this.ServiceList.Add((Service)o);
                        break;
                }
            }
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top