Xml文档 - 输出不正确 [英] Xml document - incorrect output

查看:79
本文介绍了Xml文档 - 输出不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有2个词典,并希望将内容输出为XML外部字符串格式,如下所示



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

推荐答案

仔细阅读你的代码并检查它是否做了它应该做的事情:

Read your code carefully and check if it does what it should do:
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.


全部谢谢,问题是拼写错误数据2正如Jochen指出的那样
Thanks all, problem was typo data2 as Jochen pointed out


这篇关于Xml文档 - 输出不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆