简体   繁体   English

从数据网格更新到 XML 属性列表

[英]Update to XML attribues list from datagrid

Morning:早晨:

Using the below method GetProductsPriceList() i read attributes of the selective nodes from the XML file and load it to the datagrid.使用下面的方法GetProductsPriceList()我从 XML 文件中读取选择节点的属性并将其加载到数据网格。

I edit the values of Visible & Price columns and save back to the XML File.我编辑 Visible & Price 列的值并保存回 XML 文件。

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 .我的问题是,当我尝试使用Price & Visible attributes的数据网格视图内容保存到 xml 文件时,只有一个值应用于Price & Visible的所有属性。

Expected result: Each Price & Visible vlaues from datagrid should be applied to each product of the Price & Visible attributes .预期结果:数据网格中的每个 Price & Visible 值都应应用于Price & Visible attributes的每个产品。

During debug i can see the pp.Visible & pp.Price has the right contents ie the values entered in the datagrid.在调试期间,我可以看到pp.Visible & pp.Price具有正确的内容,即在数据网格中输入的值。

I dont know what is wrong with my approcah with Foreach loop?我不知道我的 Foreach 循环方法有什么问题? How can i fix this?我怎样才能解决这个问题? thanks谢谢

Price is an attribute of ProductPrice and Visible is an attribute of ProductVisibility under Visibilities PriceProductPrice的一个attributeVisibleVisibilitiesProductVisibility的一个attribute

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 文件:

<?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>

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.您必须检查 pp 中的产品是否等于 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.如果不是,您可以设置所有使用doc.SelectNodes("/Data/Products/*")选择的产品的可见和价格属性,而不是仅在第页中设置产品。

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.在不确切了解业务 model 的情况下,我不可能确切地说出如何检查 pp 中的产品是否等于 pn 中的产品。 It could maybe go like this, if Mainprduct is the primary key of the products:它可能是这样的 go,如果 Mainprduct 是产品的主键:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM