Home > Software design >  Update to XML attribues list from datagrid
Update to XML attribues list from datagrid

Time:01-31

Morning:

Using the below method GetProductsPriceList() i read attributes of the selective nodes from the XML file and load it to the datagrid.

I edit the values of Visible & Price columns and save back to the XML File.

private ObservableCollection<Products> GetProductsPriceList()
{
    productpricelist = new ObservableCollection<Products>();
    XmlDocument doc = new XmlDocument();
    doc.Load(@"C:\xmltest\26112023.txt");
    foreach (XmlElement pn in doc.SelectNodes("/Data/Products/*"))
    {
        var productlist = new Products
        {
            Mainproduct = pn.LocalName.ToString(),
            Name = pn.GetAttribute("Name"),
            Price = pn.SelectSingleNode(".//ProductPrice/@Price")?.Value,
            Visible = pn.SelectSingleNode(".//ProductVisibility/@Visible")?.Value,
            NameIcon = pn.GetAttribute("DefaultIconName")
        };
        productpricelist.Add(productlist);
    }

    return productpricelist;
}

My issue here, when i try to save to the xml file with datagrid view contents of Price & Visible attributes only one value is applied to all the attributes of Price & Visible .

Expected result: Each Price & Visible vlaues from datagrid should be applied to each product of the Price & Visible attributes.

During debug i can see the pp.Visible & pp.Price has the right contents i.e. the values entered in the datagrid.

I dont know what is wrong with my approcah with Foreach loop? How can i fix this? thanks

Price is an attribute of ProductPrice and Visible is an attribute of ProductVisibility under Visibilities

private void Execute(object parm) //method to save back to the xml file price & visible attributes for each products 
{

    XmlDocument doc = new XmlDocument();
    doc.Load(@"C:\xmltest\26112023.txt");
    foreach (Products pp in productpricelist)
    {
        foreach (XmlElement pn in doc.SelectNodes("/Data/Products/*"))
    {

        
            foreach (XmlNode visibility in pn.SelectNodes("Visibilities"))
            {
                foreach (XmlNode productVisibilty in visibility.SelectNodes("ProductVisibility"))
                {
                    productVisibilty.Attributes["Visible"].InnerText = pp.Visible;

                    foreach (XmlNode price in productVisibilty.SelectNodes("Prices"))
                    {
                        foreach (XmlNode productPrice in price.SelectNodes("ProductPrice"))
                        {
                            productPrice.Attributes["Price"].InnerText = pp.Price;
                        }
                    }
                }
            }
        }

            }
doc.Save(@"C:\xmltest\26112023_.txt");
}

Sample XML File:

<?xml version="1.0" encoding="utf-8"?>
<Data>
  <ShippingMethodsReferences />
  <Products>
    <ProductApple ID="77f9df03-7525-44d7-b08d-dcf106b44969" Sys_Type="Library.Domain.Products.ProductApple" Sys_ImportStrategy="UpdateOnly" DefaultIconName="Xerox_10x8x2_R_Sticker.png" Profile="Fruits" DefaultOutputProfileTypeFullName="Output" Name="(10x8)x2 H Rectangular">
      <ShippingMethodPrices ImportStrategy="Replace" />
      <Visibilities ImportStrategy="Replace">
        <ProductVisibility ID="adfa5eca-3f58-4107-8738-5b9b1c0b666f" Sys_GlobalUniqueId="adfa5eca-3f58-4107-8738-5b9b1c0b666f" Sys_ReplicationId="ee46c28d-7b82-447d-a3a9-a67e0e4bbd5b" Sys_Type="ProductVisi" Activated="True" OverrideServerProductPrices="False" PhotoSource="EndUserPhotos" BackgroundColor="Default" Icon="" Image="" IsUnusableByLicense="False" MaxDate="2999-12-31" MinDate="1900-01-01" Name="" OrderableObjectVisibility="Visible" Visible="True" ReplicationId="ee46c28d-7b82-447d-a3a9-a67e0e4bbd5b">
          <Prices ImportStrategy="Replace">
            <ProductPrice ID="e558ceed-1e64-4540-8958-0203fea2b53b" Sys_GlobalUniqueId="e558ceed-1e64-4540-8958-0203fea2b53b" Sys_ReplicationId="2810c015-da9b-4aa9-ad43-ac7c02341b79" Sys_Type="Library.Domain.ProductPrice" FixFee="0" ServiceFee="0" Mode="Replace" FromQuantity="1" Price="0" ProductPriceType="PerPageQuantity" ReplicationId="2810c015-da9b-4aa9-ad43-ac7c02341b79" />
          </Prices>
        </ProductVisibility>
      </Visibilities>
    </ProductApple>
    <ProductSolo ID="7c1302d8-8832-451b-be64-c5d048d0332f" Sys_Type="Library.Domain.Products.ProductApple" Sys_ImportStrategy="UpdateOnly" DefaultIconName="Xerox_10x8x2_R_Metallic.png" Profile="Fruits" DefaultOutputProfileTypeFullName="Output" Name="(10x8)x2 H Rectangular">
      <ShippingMethodPrices ImportStrategy="Replace" />
      <Visibilities ImportStrategy="Replace">
        <ProductVisibility ID="078f6e6a-895c-4957-b808-2b38589ba4cd" Sys_GlobalUniqueId="078f6e6a-895c-4957-b808-2b38589ba4cd" Sys_ReplicationId="e0ca0706-0113-4479-ab47-59d2b14bf837" Sys_Type="ProductVisi" Activated="True" OverrideServerProductPrices="False" PhotoSource="EndUserPhotos" BackgroundColor="Default" Icon="" Image="" IsUnusableByLicense="False" MaxDate="2999-12-31" MinDate="1900-01-01" Name="" OrderableObjectVisibility="Visible" Visible="True" ReplicationId="e0ca0706-0113-4479-ab47-59d2b14bf837">
          <Prices ImportStrategy="Replace">
            <ProductPrice ID="0c14f953-9f75-4e24-9c38-245027107167" Sys_GlobalUniqueId="0c14f953-9f75-4e24-9c38-245027107167" Sys_ReplicationId="2d243d53-cd0c-4a92-adea-919a9bcb427d" Sys_Type="Library.Domain.ProductPrice" FixFee="0" ServiceFee="0" Mode="Replace" FromQuantity="1" Price="0" ProductPriceType="PerPageQuantity" ReplicationId="2d243d53-cd0c-4a92-adea-919a9bcb427d" />
          </Prices>
        </ProductVisibility>
      </Visibilities>
    </ProductSolo>
    </Data>
</Products>

CodePudding user response:

Before

            foreach (XmlNode visibility in pn.SelectNodes("Visibilities"))
            {
                foreach (XmlNode productVisibilty in visibility.SelectNodes("ProductVisibility"))
                {
                    productVisibilty.Attributes["Visible"].InnerText = pp.Visible;

                    foreach (XmlNode price in productVisibilty.SelectNodes("Prices"))
                    {
                        foreach (XmlNode productPrice in price.SelectNodes("ProductPrice"))
                        {
                            productPrice.Attributes["Price"].InnerText = pp.Price;
                        }
                    }
                }
            }

you have to check, if the product in pp equals the product in pn. If not you set Visible and Price Attribute of all Products selected with doc.SelectNodes("/Data/Products/*") instead of only the product in pp.

Without knowing the business model exactly, it's not possible for me to say exactly how to check if the product in pp is equal to the product in pn. It could maybe go like this, if Mainprduct is the primary key of the products:

    if( pp.Mainproduct == pn.LocalName.ToString())
    {
        // TODO: Insert here the code from above
    }
  • Related