我之前因为类似的情况而发布了。现在我在下一个港口,现在不幸的是它有点复杂。问题是我完全将类转换为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说绝对不适合这样的东西,但是当我打印所有东西时,无论如何都指望用指定的&lt; ...&gt;不可能发生......转换为XML时没有问题,但遗憾的是,将XML代码重新放回到类中是不可行的,因为财务报表都具有相同的名称(和)。 因此,有一种方法可以序列化(但显然没有)XmlElement WITH ATTRIBUTE产品,或者我是否需要更好的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" };
...
}
答案 0 :(得分: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);
}
}
}