0

因为前段时间有一个类似的案例,我已经发布了。现在我在下一个港口,不幸的是它现在有点复杂。问题是我将一个类完全转换为XML,这种格式必须是:

<ENTITIES>
  <ENTITY name="S40">
    <ATTRIBUTES>
      <ATTRIBUTE name="INSOBJECT">X</ATTRIBUTE>
      <ATTRIBUTE name="INSOBJECTTYP">X</ATTRIBUTE>
    </ATTRIBUTES>
  </ENTITY>
  <ENTITY name="S41">
    <ATTRIBUTES>
      <ATTRIBUTE name="INSOBEZ">X</ATTRIBUTE>
      <ATTRIBUTE name="/SDE/CD_FDATE">X</ATTRIBUTE>
      <ATTRIBUTE name="/SDE/CD_COL_FORM">X</ATTRIBUTE>
      <ATTRIBUTE name="/SDE/CD_APPLNR">X</ATTRIBUTE>
      <ATTRIBUTE name="/SDE/CD_POLBEG">X</ATTRIBUTE>
    </ATTRIBUTES>
  </ENTITY>

问题是这里看起来不错,因为每个变量都转换为 XML 格式,同时需要一个名为“ATTRIBUTES”的新 XmlElement。但是因为我可以创建一个新的项目 WITH 属性,我想到了一个 String.Replace ...不幸的是,我有,但问题是并且不能轻易替换,因为有几个不同的。不幸的是,我也无法切换到 XML 格式,因为这种格式是给我的,并且已经被其他人使用了。

我现在的方式:

[System.Xml.Serialization.XmlRoot("ENTITIES")]
public class ivRequester
{

    private s_40[] s40;
    private s_41[] s41;
    private s_42[] s42;
    private s_43[] s43;
    private s_45[] s45;
    private c_99[] c99;

    public s_40[] S40
    {
        get { return s40; }
        set { s40 = value; }
    }
    public s_41[] S41
    {
        get { return s41; }
        set { s41 = value; }
    }
    public s_42[] S42
    {
        get { return s42; }
        set { s42 = value; }
    }
    public s_43[] S43
    {
        get { return s43; }
        set { s43 = value; }
    }
    public s_45[] S45
    {
        get { return s45; }
        set { s45 = value; }
    }
    public c_99[] C99
    {
        get { return c99; }
        set { c99 = value; }
    }

    public class s_40
    {
        public string INSOBJECT = "X";
        public string INSOBJECTTYP = "X";
        public string CD_OLDOBJNR = "X";
    }

    public class s_41
    {
        public string INSOBEZ = "X";
        public string slashSDEslashCD_FDATE = "X";
        public string slashSDEslashCD_COL_FORM = "X";
        public string slashSDEslashCD_APPLNR = "X";
    }

    public class s_42
    {
        public string PARTNER = "X";
        public string PARTNER_EXT = "X";
        public string PARTNER_ADEXT = "X";
    }

    public class s_43
    {
        public string partner = "X";
    }

    public class s_45
    {
        public string PARTNER = "X";
        public string PARTNER_EXT = "X";
        public string PARTNER_ADEXT = "X";
    }

    public class c_99
    {
        public string CODE = "X";
        public string MV_MESSAGE = "X";
        public string MV_MSGTY = "X";
    }

    public string XMLout()
    {
        string returnstring = Program.ObjectToXml(this);

        Console.WriteLine(returnstring);

        string[] toreplace = {"INSOBJECT", "INSOBJECTTYP", "CD_OLDOBJNR", "CD_BRANCH", "PARTNER", "PARTNER_EXT", "PARTNER_ADEXT", ... };

        for (int i = 0; i < toreplace.Length; i++)
        {
            returnstring = returnstring.Replace("<" + toreplace[i] + ">", "<ATTRIBUTE name=\"" + toreplace[i] + "\">");
            returnstring = returnstring.Replace("</" + toreplace[i] + ">", "</ATTRIBUTE>");
            returnstring = returnstring.Replace("<" + toreplace[i] + "/>", "<ATTRIBUTE name=\"" + toreplace[i] + "\"/>");
            returnstring = returnstring.Replace("<" + toreplace[i] + " />", "<ATTRIBUTE name=\"" + toreplace[i] + "\"/>");
        }
        returnstring = returnstring.Replace("slashSDEslash", "/SDE/");
        returnstring = returnstring.Replace("<ENTITIES xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">", "<ENTITIES>");
        string[] EntitiesToReplace = { "S40", "S41", "S42", "S43", "S45", "C99" };
        for (int i = 0; i < EntitiesToReplace.Length; i++)
        {
            returnstring = returnstring.Replace("<" + EntitiesToReplace[i] + ">", "<ENTITY name=\"" + EntitiesToReplace[i] + "\">");
            returnstring = returnstring.Replace("</" + EntitiesToReplace[i] + ">", "</ENTITY>");
            returnstring = returnstring.Replace("<" + EntitiesToReplace[i] + "/>", "<ENTITY name=\"" + EntitiesToReplace[i] + "\"/>");
            returnstring = returnstring.Replace("<" + EntitiesToReplace[i] + " />", "<ENTITY name=\"" + EntitiesToReplace[i] + "\"/>");
        }
        string[] ChangeToAttributes = { "s_40", "s_41", "s_42", "s_43", "s_45", "c_99" };
        for (int i = 0; i < ChangeToAttributes.Length; i++)
        {
            returnstring = returnstring.Replace("<" + ChangeToAttributes[i] + ">", "<ATTRIBUTES>");
            returnstring = returnstring.Replace("</" + ChangeToAttributes[i] + ">", "</ATTRIBUTES>");
            returnstring = returnstring.Replace("<" + ChangeToAttributes[i] + "/>", "<ATTRIBUTES/>");
            returnstring = returnstring.Replace("<" + ChangeToAttributes[i] + " />", "<ATTRIBUTES/>");
        }

        return returnstring;
    }

    public ivRequester XMLin(string XMLCode)
    {
        Console.WriteLine(XMLCode);
        string returnstring = XMLCode;

        string[] toreplace = { "INSOBJECT", "INSOBJECTTYP", "CD_OLDOBJNR", "CD_BRANCH", "PARTNER", "PARTNER_EXT", "PARTNER_ADEXT", "VALID_FROM", "AEND_TMS", "FUNCTION", "USERNAME", "HOSTUSER", "SOURCE", "INSOBEZ", ... };

        for (int i = 0; i < toreplace.Length; i++)
        {
            returnstring = returnstring.Replace("<ATTRIBUTE name=\"" + toreplace[i] + "\">", "<" + toreplace[i] + ">");
            returnstring = returnstring.Replace("</ATTRIBUTE>", "</" + toreplace[i] + ">");
            returnstring = returnstring.Replace("<ATTRIBUTE name=\"" + toreplace[i] + "\"/>", "<" + toreplace[i] + "/>");
            returnstring = returnstring.Replace("<ATTRIBUTE name=\"" + toreplace[i] + "\"/>", "<" + toreplace[i] + " />");
        }
        returnstring = returnstring.Replace("/SDE/", "slashSDEslash");
        returnstring = returnstring.Replace("<ENTITIES>", "<ENTITIES xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
        string[] EntitiesToReplace = { "S40", "S41", "S42", "S43", "S45", "C99" };
        for (int i = 0; i < EntitiesToReplace.Length; i++)
        {
            returnstring = returnstring.Replace("<ENTITY name=\"" + EntitiesToReplace[i] + "\">", "<" + EntitiesToReplace[i] + ">");
            returnstring = returnstring.Replace("</ENTITY>", "</" + EntitiesToReplace[i] + ">");
            returnstring = returnstring.Replace("<ENTITY name=\"" + EntitiesToReplace[i] + "\"/>", "<" + EntitiesToReplace[i] + "/>");
            returnstring = returnstring.Replace("<ENTITY name=\"" + EntitiesToReplace[i] + "\"/>", "<" + EntitiesToReplace[i] + " />");
        }

        ivRequester test = new ivRequester();//<--Search for better way to do that... i want to give THIS as return, but that is coming next or somthing else ;)
        try
        {
            XmlSerializer serializer = new XmlSerializer(this.GetType());

            test = (ivRequester)serializer.Deserialize(new StringReader(returnstring)); //<--Search for better way to do that... i want to give THIS as return, but that is coming next or somthing else ;)
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return test;
        }
        return test;
    }

我以前用 [XmlAttribute ()] 和 [XmlElement ()] 尝试过,但不幸的是我没有进一步的类似的东西。当时间不是那么稀缺的时候,String.Replace 说绝对不适合这样的事情,但是当我打印所有东西时,我想自己无论如何用 <...> 指定不可能发生......当转换为XML 没问题,但不幸的是,将 XML 代码重新写入类是不可行的,因为财务报表都具有相同的名称(和)。因此,要么有一种方法可以序列化(但显然没有)带有属性的 XmlElement 产品,还是我需要一个更好的 XML 解析器,因为节点甚至无法重命名它。-.-

我希望有人给我小费,Szandor

PS:我还尝试了一个名为 ATTRIBUTE 的额外类,然后您必须输入属性名称和值的名称。不幸的是,当第二个 ATTRIBUTES 时间设置为 XmlElement 时,这不起作用,它会崩溃……当我调用 Attribute2 时,它会崩溃,但由于所有 HOT 必须属性,不幸的是无法使用:(

public class Attribute
{
    [XmlAttribute("name")]
    public string name;
    [XmlText()]
    public string wert;
}

public class s_40
{
    [XmlElement("ATTRIBUTE")]
    public Attribute INSOBJECT2 = new Attribute() { name = "INSOBJECT", wert = "test" };
    [XmlElement("ATTRIBUTE")]
    public Attribute INSOBJECTTYP2 = new Attribute() { name = "INSOBJECTTYP", wert = "test" };
    ...
}
4

1 回答 1

0

已经通过这种方式解决了,代码真的不多,而且有效:)

XmlDocument OriginalXML = new XmlDocument();
XmlDocument ConvertedXML = new XmlDocument();
OriginalXML.LoadXml(XMLCode);
ConvertedXML.LoadXml("<ENTITIES xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"></ENTITIES>");

XmlNode rootNodeOriginal = OriginalXML.SelectSingleNode("/ENTITIES");
XmlNode rootNodeConverted = ConvertedXML.SelectSingleNode("/ENTITIES");
string EntityName = "";
string EntityName2 = "";
foreach (XmlNode EntityNodes in rootNodeOriginal.ChildNodes)
{
    if (EntityNodes.Name == "ENTITY")
    {
        EntityName = EntityNodes.Attributes[0].InnerXml;
        EntityName2 = EntityName.Replace("S","s_").Replace("C","c_");
        XmlNode newNode = ConvertedXML.CreateElement(EntityName);
        rootNodeConverted.AppendChild(newNode);
        ConvertedXML.SelectSingleNode("/ENTITIES/" + EntityName).AppendChild(ConvertedXML.CreateElement(EntityName2));

        if (EntityNodes.FirstChild.Name == "ATTRIBUTES")
            foreach (XmlNode ChildNodeToChange in EntityNodes.FirstChild.ChildNodes)
                if (ChildNodeToChange.Name == "ATTRIBUTE")
                {
                    XmlNode newChildNodes = ConvertedXML.CreateElement(ChildNodeToChange.Attributes[0].InnerXml.Replace("/","slash"));
                    newChildNodes.InnerXml = ChildNodeToChange.InnerXml;
                    ConvertedXML.SelectSingleNode("/ENTITIES/" + EntityName + "/" + EntityName2).AppendChild(newChildNodes);
                }
    }
}
于 2012-06-05T15:35:21.413 回答