GrayFullBuster
GrayFullBuster

Reputation: 1063

XML output contains duplicate values after creating it using C# Linq to XML

Here is my code for creating an XML file

public void CreateXml(string[] names, string[] values, string type)
{
        XElement xml = new XElement("Transaction",
            new XElement("TransactionType", type));
        foreach (var i in names)
        {
            foreach (var o in values)
            {
                xml.Add(new XElement(i.Replace(" ", string.Empty), o));
            }
        }
        xml.Save("C:\\Users\\PHWS13\\Desktop\\sample.xml");
}

And my output looks like this

<?xml version="1.0" encoding="UTF-8"?>
<Transaction> 
    <TransactionType>Void</TransactionType> 
    <Zip>1</Zip>
     <Zip>2</Zip> 
     <PNRef>1</PNRef> 
     <PNRef>2</PNRef> 
 </Transaction>

But it is not right, I am expecting more like this

<?xml version="1.0" encoding="UTF-8"?>
<Transaction> 
    <TransactionType>Void</TransactionType> 
    <Zip>1</Zip>
     <PNRef>2</PNRef> 
 </Transaction>

As I have noticed the values are correct but it i just duplicates, How can I fix this?

string[] textBoxNamesArray = flowLayoutPanelText.Controls.OfType<TextBox>()
                                  .Select(r => r.Name)
                                  .ToArray();

string[] textBoxTextsArray = flowLayoutPanelText.Controls.OfType<TextBox>()
                                  .Select(r => r.Text)
                                  .ToArray();

Upvotes: 1

Views: 174

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

You shouldn't use two nested loops then: two nested loops create a Cartesian product (i.e. every name is paired up with every value).

If you use a single loop that goes through names and values at the same time, you'd get your expected output.

If you are using .NET 4, you could use Zip method, like this:

xml.Add(
    names
    .Zip(values, (n,v) => new XElement(n.Name(" ", string.Empty), v))
    .ToArray()
);

Upvotes: 4

Ravi Y
Ravi Y

Reputation: 4376

Try this using a dictionary.

public void CreateXml(Dictionary<string, string> myNameValuePairs, string type)
{
    XElement xml = new XElement("Transaction",
        new XElement("TransactionType", type));
    foreach (var key in names)
    {
        xml.Add(new XElement(key.Replace(" ", string.Empty), myNameValuePairs[key]));
     }
    xml.Save("C:\\Users\\PHWS13\\Desktop\\sample.xml");
}

Edit: Workaround using your method signature as above. But this works if an only if values[i] is the corresponding value for names[i]. I would not advise this unless you are sure about the array lengths and contens of names and values.

public void CreateXml(string[] names, string[] values, string type)
{
    XElement xml = new XElement("Transaction",
        new XElement("TransactionType", type));
    foreach (var i = 0; i < names.length; i++)
    {
        xml.Add(new XElement(names[i].Replace(" ", string.Empty), values[i]));
    }
    xml.Save("C:\\Users\\PHWS13\\Desktop\\sample.xml");
}

Upvotes: 2

Xavier Poinas
Xavier Poinas

Reputation: 19733

Hard to tell without knowing how you structure the data in your 2 arrays, but I would guess:

    for (int i = 0; i < names.Length; i++)
    {
        xml.Add(new XElement(names[i].Replace(" ", string.Empty), values[i]));
    }

Upvotes: 3

Related Questions