Click here to Skip to main content
15,921,840 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I have 2 dictionaries and want to output there contents into an XML outer string format like the below

<?xml version="1.0" encoding="ISO-8859-1"?>
<WIRE_DUMP_FILE>
<BPReferences>
<DATA>
<DEVICE NAME="Device" REFX1="-22305.122" REFY1="24343.124" REFX2="" REFY2="" />
<DEVICE NAME="MEMS" REFX1="-21482.655" REFY1="23124.96" REFX2="-21588.708" REFY2="23901.819" />
<DEVICE NAME="ASIC" REFX1="-20438.966" REFY1="23751.677" REFX2="-21097.992" REFY2="23238.713" />
</DATA>
</BPReferences>
<BPData>
<DATA>
<WIRE NUMBER="1" GROUP="1" BALL_BONDED_ON="ASIC" BALL_BOND_POS_X="-20888.715" BALL_BOND_POS_Y="23716.429" WEDGE_BONDED_ON="Device" WEDGE_BOND_POS_X="-20851.959" WEDGE_BOND_POS_Y="24138.604" />
<WIRE NUMBER="2" GROUP="1" BALL_BONDED_ON="ASIC" BALL_BOND_POS_X="-20813.035" BALL_BOND_POS_Y="23716.774" WEDGE_BONDED_ON="Device" WEDGE_BOND_POS_X="-20716.007" WEDGE_BOND_POS_Y="24160.933" />
<WIRE NUMBER="3" GROUP="3" BALL_BONDED_ON="MEMS" BALL_BOND_POS_X="-21551.031" BALL_BOND_POS_Y="23868.96" WEDGE_BONDED_ON="MEMS" WEDGE_BOND_POS_X="-21580.768" WEDGE_BOND_POS_Y="23883.268" />
<WIRE NUMBER="4" GROUP="2" BALL_BONDED_ON="ASIC" BALL_BOND_POS_X="-21078.331" BALL_BOND_POS_Y="23646.876" WEDGE_BONDED_ON="MEMS" WEDGE_BOND_POS_X="-21551.031" WEDGE_BOND_POS_Y="23868.96" />
<WIRE NUMBER="5" GROUP="3" BALL_BONDED_ON="MEMS" BALL_BOND_POS_X="-21541.046" BALL_BOND_POS_Y="23183.188" WEDGE_BONDED_ON="MEMS" WEDGE_BOND_POS_X="-21571.548" WEDGE_BOND_POS_Y="23170.593" />
<WIRE NUMBER="6" GROUP="2" BALL_BONDED_ON="ASIC" BALL_BOND_POS_X="-21077.999" BALL_BOND_POS_Y="23382.038" WEDGE_BONDED_ON="MEMS" WEDGE_BOND_POS_X="-21541.046" WEDGE_BOND_POS_Y="23183.188" />
<WIRE NUMBER="7" GROUP="1" BALL_BONDED_ON="ASIC" BALL_BOND_POS_X="-20535.508" BALL_BOND_POS_Y="23717.582" WEDGE_BONDED_ON="Device" WEDGE_BOND_POS_X="-20598.69" WEDGE_BOND_POS_Y="24140.724" />
<WIRE NUMBER="8" GROUP="1" BALL_BONDED_ON="ASIC" BALL_BOND_POS_X="-20469.177" BALL_BOND_POS_Y="23717.632" WEDGE_BONDED_ON="Device" WEDGE_BOND_POS_X="-20467.223" WEDGE_BOND_POS_Y="24142.328" />
</DATA>
</BPData>
</WIRE_DUMP_FILE>


The problem is that
<BPReferences>
element is being closed at the end of the document.

What I have tried:

public string getXmlAsString()
{
    XmlDocument xDoc = new XmlDocument();

    //XmlNode docNode = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
    //xDoc.AppendChild(docNode);

    XmlNode root = xDoc.CreateElement("WIRE_DUMP_FILE");
    xDoc.AppendChild(root);

    XmlNode bpReferences = xDoc.CreateElement("BPReferences");
    root.AppendChild(bpReferences);

    XmlNode data = xDoc.CreateElement("DATA");

    foreach (KeyValuePair<String, Device> kvp in _devices)
    {
        XmlNode device = xDoc.CreateElement("DEVICE");

        XmlAttribute name = xDoc.CreateAttribute("NAME");
        name.Value = kvp.Value.DeviceName;

        device.Attributes.Append(name);

        XmlAttribute refx1 = xDoc.CreateAttribute("REFX1");
        refx1.Value = kvp.Value.Ref1.X.ToString();
        device.Attributes.Append(refx1);

        XmlAttribute refy1 = xDoc.CreateAttribute("REFY1");
        refy1.Value = kvp.Value.Ref1.Y.ToString();
        device.Attributes.Append(refy1);

        XmlAttribute refx2 = xDoc.CreateAttribute("REFX2");
        refx2.Value = kvp.Value.Ref2.Value.X.ToString();
        device.Attributes.Append(refx2);

        XmlAttribute refy2 = xDoc.CreateAttribute("REFY2");
        refy2.Value = kvp.Value.Ref2.Value.Y.ToString();
        device.Attributes.Append(refy2);

        data.AppendChild(device);
    }

    bpReferences.AppendChild(data);


    XmlNode subNode2 = xDoc.CreateElement("BPData");
   root.PrependChild(subNode2);

    XmlNode data2 = xDoc.CreateElement("DATA");
    subNode2.AppendChild(data2);

    foreach (KeyValuePair<Int16, Wire> kvp in _wires)
    {
        XmlNode wire = xDoc.CreateElement("WIRE");

        XmlAttribute number = xDoc.CreateAttribute("NUMBER");
        number.Value = kvp.Value.WireNumber.ToString();

        wire.Attributes.Append(number);

        XmlAttribute group = xDoc.CreateAttribute("GROUP");
        group.Value = kvp.Value.WireGroup.ToString();
        wire.Attributes.Append(group);

        XmlAttribute ballBondedOn = xDoc.CreateAttribute("BALL_BONDED_ON");
        ballBondedOn.Value = kvp.Value.BallBondedOn.DeviceName;
        wire.Attributes.Append(ballBondedOn);

        XmlAttribute ballBondPosX = xDoc.CreateAttribute("BALL_BOND_POS_X");
        ballBondPosX.Value = kvp.Value.BallBond.X.ToString();
        wire.Attributes.Append(ballBondPosX);

        XmlAttribute ballBondPosY = xDoc.CreateAttribute("BALL_BOND_POS_Y");
        ballBondPosY.Value = kvp.Value.BallBond.Y.ToString();
        wire.Attributes.Append(ballBondPosY);

        XmlAttribute wedgeBondedOn = xDoc.CreateAttribute("WEDGE_BONDED_ON");
        wedgeBondedOn.Value = kvp.Value.WedgeBondedOn.DeviceName;
        wire.Attributes.Append(wedgeBondedOn);

        XmlAttribute wedgeBondPosX = xDoc.CreateAttribute("WEDGE_BOND_POS_X");
        wedgeBondPosX.Value = kvp.Value.WedgeBond.X.ToString();
        wire.Attributes.Append(wedgeBondPosX);

        XmlAttribute wedgeBondPosY = xDoc.CreateAttribute("WEDGE_BOND_POS_Y");
        wedgeBondPosY.Value = kvp.Value.WedgeBond.Y.ToString();
        wire.Attributes.Append(wedgeBondPosY);
        data.AppendChild(wire);
    }
    return xDoc.OuterXml;
}
Posted
Updated 24-Jan-17 6:37am

Read your code carefully and check if it does what it should do:
C#
XmlNode data2 = xDoc.CreateElement("DATA");
// This should be moved behind the loop
//subNode2.AppendChild(data2);

foreach (KeyValuePair<Int16, Wire> kvp in _wires)
{
    // ...
    // This should be data2
    //data.AppendChild(wire);
    data2.AppendChild(wire);
}
// Moved to here
subNode2.AppendChild(data2);

There may be more errors that you should be able to find yourself.
 
Share this answer
 
Comments
Richard Deeming 24-Jan-17 11:38am    
I don't think it matters whether you add the node to the parent before or after adding its children. Leaving subNode2.AppendChild(data2) before the foreach loop shouldn't make any difference.
Jochen Arndt 24-Jan-17 11:51am    
You are probably right. But using it in this way makes it more clear what happens and when (the sequence of calls defines the order; not a problem here).

But it is essential to append to the corresponding node (data2 instead of data here).
Thanks all, problem was typo data2 as Jochen pointed out
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900