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

fdx2calv  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(149)

上午:
使用下面的方法GetProductsPriceList(),我从XML文件中读取选择节点的属性,并将其加载到数据网格中。
我编辑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;
}

我这里的问题是,当我尝试将Price & Visible attributes的datagrid视图内容保存到xml文件时,只有一个值应用于Price & Visible的所有属性。

    • 预期结果:**来自datagrid的每个价格和可视值应应用于Price & Visible attributes的每个产品。

在调试期间,我可以看到pp.Visiblepp.Price具有正确的内容,即在数据网格中输入的值。
我不知道我的approcah与foreach循环有什么问题?我如何修复这个?谢谢
PriceProductPriceattribute,并且VisibleProductVisibilityVisibilities下的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");
}

示例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>
q35jwt9p

q35jwt9p1#

之前

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;
                        }
                    }
                }
            }

您需要检查pp中的产品是否等于pn中的产品,如果不相等,您需要设置doc.SelectNodes("/Data/Products/*")选中的所有产品的可见性和价格属性,而不仅仅是pp中的产品。
在不知 prop 体业务模式的情况下,我不可能说清楚如何检查pp中的产品是否等于pn中的产品,如果Mainproduct是产品的主键,可能会是这样的:

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

相关问题