从.xml中读取产品信息并将其写入另一个.xml

时间:2014-01-14 15:39:10

标签: c# xml dom

A在XML中有一个非常简单的产品列表。我需要阅读那些产品,并将堆栈小于或等于5的产品写入另一个XML文件。

来自第一个XML文件的示例:

<product id="1">
    <name>Name 1</name>
    <price>12</price>
    <stack>10</stack>
</product>
<product id="2">
    <name>Name 2</name>
    <price>10</price>
    <stack>10</stack>
</product>
<product id="3">
    <name>Name 3</name>
    <price>8</price>
    <stack>10</stack>
</product>  

我试图实现这样的目标:

public void FilterProducts(string input, string output)
    {
        if (input != null)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(input);

            XmlElement root = doc.DocumentElement;

            XmlNodeList nameList = root.GetElementsByTagName("name");
            XmlNodeList priceList = root.GetElementsByTagName("price");
            XmlNodeList stackList = root.GetElementsByTagName("stack");

            for (int i = 0; i < nameList.Count; i++)
            {
                double quantityOnStack = Convert.ToDouble(stackList[i].InnerText);

                if (quantityOnStack >= 5)
                {
                    XmlElement product= doc.CreateElement("product");
                    product.SetAttribute("id", (i + 1).ToString());

                    XmlElement name = doc.CreateElement("name");
                    name.InnerText = nameList[i].InnerText;
                    product.AppendChild(name);

                    XmlElement price = doc.CreateElement("price");
                    price.InnerText = priceList[i].InnerText;
                    product.AppendChild(price);

                    XmlElement stack = doc.CreateElement("stack");
                    stack.InnerText = stackList[i].InnerText;
                    product.AppendChild(stack);


                    XmlTextWriter tw = new XmlTextWriter(output, null);
                    tw.Formatting = Formatting.Indented;
                    doc.WriteContentTo(tw);

                    tw.Close();
                }
            }
        }
        else
        {
            MessageBox.Show("File doesn't exist.");
        }
    }

第二个XML文件(应该被过滤)与第一个XML文件完全相同。所以基本上if语句不能以某种方式工作。我也有列表中的堆栈小于5的产品。有什么提示吗?

3 个答案:

答案 0 :(得分:1)

较小或等于5

好了...

if (quantityOnStack >= 5)

企业风险管理...

<stack>10</stack>

对于quantityOnStack 大于或等于,您的if语句返回true为5.示例XML中每个stack中的product大于5 ,所以当然所得到的XML都是以上所有。

你的意思是写:

if (quantityOnStack <= 5)

答案 1 :(得分:0)

试试这个。

        XDocument doc = XDocument.Load("products.xml");

        var products = from p in doc.Descendants("product")
                       where Double.Parse(p.Element("stack").Value) <= 5
                       select p;

        XDocument output = new XDocument();
        XElement root = new XElement("root");
        output.Add(root);
        foreach(var e in products)
        {
            root.Add(e);
        }

        output.Save("found.xml");

使用XmlDocument

        XmlDocument doc = new XmlDocument();
        doc.Load("products.xml");

        XmlNodeList products = doc.GetElementsByTagName("product");

        XmlDocument output = new XmlDocument();
        XmlElement root = output.CreateElement("root");
        output.AppendChild(root);
        foreach(XmlNode e in products)
        {
            if (Double.Parse(e.ChildNodes[2].InnerText) <= 5)
            {
                XmlNode imported = output.ImportNode(e, true);
                root.AppendChild(imported);
            }
        }

        output.Save("found.xml");

答案 2 :(得分:0)

首先,您将输入文档加载到doc变量

doc.Load(input);

然后将doc元素写入文件

doc.WriteContentTo(tw);

您基本上是在输出文件中直接编写输入文档。

您需要创建第二个XmlDocument,在其中添加已过滤的产品列表,然后使用Save方法将列表保存到输出文件

public void FilterProducts(string input, string output)
{
    if (input != null)
    {
        XmlDocument doc = new XmlDocument();

        doc.Load(input);

        XmlElement root = doc.DocumentElement;

        XmlNodeList nameList = root.GetElementsByTagName("name");
        XmlNodeList priceList = root.GetElementsByTagName("price");
        XmlNodeList stackList = root.GetElementsByTagName("stack");

        XmlDocument outputDoc = new XmlDocument();
        XmlElement products = outputDoc.CreateElement("products");
        outputDoc.AppendChild(products);

        for (int i = 0; i < nameList.Count; i++)
        {
            double quantityOnStack = Convert.ToDouble(stackList[i].InnerText);

            if (quantityOnStack >= 5)
            {
                XmlElement product= outputDoc.CreateElement("product");
                product.SetAttribute("id", (i + 1).ToString());

                XmlElement name = outputDoc.CreateElement("name");
                name.InnerText = nameList[i].InnerText;
                product.AppendChild(name);

                XmlElement price = outputDoc.CreateElement("price");
                price.InnerText = priceList[i].InnerText;
                product.AppendChild(price);

                XmlElement stack = outputDoc.CreateElement("stack");
                stack.InnerText = stackList[i].InnerText;
                product.AppendChild(stack);

                products.AppendChild(product);

            }
        }
        outputDoc.Save(output);
    }
    else
    {
        MessageBox.Show("File doesn't exist.");
    }
}