类说明如下:ElementName中有两个全是等于“总计”的,序列化时候提示 反射类型 时出错
请教大家有遇上同样问题的吗?
namespace Contecmed.XML
{
/// <summary>
/// 室性早搏段
/// </summary>
public class VEXml
{
//室性总数
private string totalVE = null;
[XmlElement(ElementName = "室性总数")]
public string TotalVE
{
get { return totalVE; }
set { totalVE = value; }
} //室性成对数
private string totalVEPair = null;
[XmlElement(ElementName = "室性成对数")]
public string TotalVEPair
{
get { return totalVEPair; }
set { totalVEPair = value; }
} //室性速
private VERunXml veRun = null;
[XmlElement(ElementName = "室性速")]
public VERunXml VERun
{
get { return veRun; }
set { veRun = value; }
} //室性千分比
private string vePerMil = null;
[XmlElement(ElementName = "室性千分比")]
public string VEPerMil
{
get { return vePerMil; }
set { vePerMil = value; }
} //二联律总数
private string totalVEBig = null;
[XmlElement(ElementName = "二联律总数")]
public string TotalVEBig
{
get { return totalVEBig; }
set { totalVEBig = value; }
} //总计
private string sumVEBig = null;
[XmlElement(ElementName = "总计")]
public string SumVEBig
{
get { return sumVEBig; }
set { sumVEBig = value; }
} //三联律总数
private string totalVETrig = null;
[XmlElement(ElementName = "三联律总数")]
public string TotalVETrig
{
get { return totalVETrig; }
set { totalVETrig = value; }
} //总计
private string sumVETrig = null;
[XmlElement(ElementName = "总计")]
public string SumVETrig
{
get { return sumVETrig; }
set { sumVETrig = value; }
} //一分最大室性数
private string maxMinuteVECount = null;
[XmlElement(ElementName = "一分最大室性数")]
public string MaxMinuteVECount
{
get { return maxMinuteVECount; }
set { maxMinuteVECount = value; }
}
}
}
请教大家有遇上同样问题的吗?
namespace Contecmed.XML
{
/// <summary>
/// 室性早搏段
/// </summary>
public class VEXml
{
//室性总数
private string totalVE = null;
[XmlElement(ElementName = "室性总数")]
public string TotalVE
{
get { return totalVE; }
set { totalVE = value; }
} //室性成对数
private string totalVEPair = null;
[XmlElement(ElementName = "室性成对数")]
public string TotalVEPair
{
get { return totalVEPair; }
set { totalVEPair = value; }
} //室性速
private VERunXml veRun = null;
[XmlElement(ElementName = "室性速")]
public VERunXml VERun
{
get { return veRun; }
set { veRun = value; }
} //室性千分比
private string vePerMil = null;
[XmlElement(ElementName = "室性千分比")]
public string VEPerMil
{
get { return vePerMil; }
set { vePerMil = value; }
} //二联律总数
private string totalVEBig = null;
[XmlElement(ElementName = "二联律总数")]
public string TotalVEBig
{
get { return totalVEBig; }
set { totalVEBig = value; }
} //总计
private string sumVEBig = null;
[XmlElement(ElementName = "总计")]
public string SumVEBig
{
get { return sumVEBig; }
set { sumVEBig = value; }
} //三联律总数
private string totalVETrig = null;
[XmlElement(ElementName = "三联律总数")]
public string TotalVETrig
{
get { return totalVETrig; }
set { totalVETrig = value; }
} //总计
private string sumVETrig = null;
[XmlElement(ElementName = "总计")]
public string SumVETrig
{
get { return sumVETrig; }
set { sumVETrig = value; }
} //一分最大室性数
private string maxMinuteVECount = null;
[XmlElement(ElementName = "一分最大室性数")]
public string MaxMinuteVECount
{
get { return maxMinuteVECount; }
set { maxMinuteVECount = value; }
}
}
}
private string sumVEBig = null;
[XmlElement(ElementName = "总计")]
public string SumVEBig
{
get { return sumVEBig; }
set { sumVEBig = value; }
}/总计
private string sumVETrig = null;
[XmlElement(ElementName = "总计")]
public string SumVETrig
{
get { return sumVETrig; }
set { sumVETrig = value; }
}
public string SumVETrig
{
get { return sumVETrig; }
set { sumVETrig = value; }
}分别为两个总计加上不同命名空间。
我按您说的加上了。运行没报错,但解析XML的内容为空。事例如下:
//总计
private string sumVEBig = null;
[XmlElement(ElementName = "总计", Namespace = "SumVEBig1")]
public string SumVEBig
{
get { return sumVEBig; }
set { sumVEBig = value; }
} //总计
private string sumVETrig = null;
[XmlElement(ElementName = "总计",Namespace = "SumVEBig2")]
public string SumVETrig
{
get { return sumVETrig; }
set { sumVETrig = value; }
}XML文件是:
<室上性早搏>
<总计>123</总计>
<总计>456</总计>
</室上性早搏>然后一个总计的值都没得到。是空的字符串,是我哪没写对吗。多谢啊。我原来是delphi出身的,C#才接楚。
<室上性早搏>
<SumVEBig1:总计>123</SumVEBig1:总计>
<SumVEBig2:总计>456</SumVEBig2:总计>
</室上性早搏>从xml反序列化,人都分不出来哪个是哪个,机器咋区分。
你能否贴一下厂家给出的XML样本,因为XML和对应CLASS是自相矛盾的
这是文件主报告.xml中的一段,只要用C#能解出来就行。这有两个元素全 总计 C# 代码是我自己写的,这个XML文件是厂家给的,我不能改。其实就是当一个XML出现两个同名节点时候如何能解析出来,原来我用Delphi也不会序列化,就是用个StringList,从上到下一条一条的顺序解析的,也不存在同名节点这个说法。但用C#序列化或反序列化就不行了。
这是XML文件的内容:
<室上性早搏>
<室上性总数>196</室上性总数>
<室上性成对数>5</室上性成对数>
<室上性速>
<室上速总数>3</室上速总数>
<最长>4</最长>
<发生>12:46:57</发生>
</室上性速>
<室上性千分比>1</室上性千分比>
<二联律总数>0</二联律总数>
<总计>123</总计>
<三联律总数>0</三联律总数>
<总计>456</总计>
<一分最大室上性数>6</一分最大室上性数>
</室上性早搏>这是SVEXML.CS 文件代码的内容,就是两个 总计 解析不过去,我想知道C#能不能有办法解析XML文件有两个一样名称元素,多谢啊namespace Contecmed.XML
{
/// <summary>
/// 室上性早搏段
/// </summary>
public class SVEXml
{
//室上性总数
private string totalSVE = null;
[XmlElement(ElementName = "室上性总数")]
public string TotalSVE
{
get { return totalSVE; }
set { totalSVE = value; }
} //室上性成对数
private string totalSVEPair = null;
[XmlElement(ElementName = "室上性成对数")]
public string TotalSVEPair
{
get { return totalSVEPair; }
set { totalSVEPair = value; }
} //室上性速
private SVERunXml sveRun= null;
[XmlElement(ElementName = "室上性速")]
public SVERunXml SVERun
{
get { return sveRun; }
set { sveRun = value; }
} //室上性千分比
private string svePerMil = null;
[XmlElement(ElementName = "室上性千分比")]
public string SVEPerMil
{
get { return svePerMil; }
set { svePerMil = value; }
} //二联律总数
private string totalSVEBig = null;
[XmlElement(ElementName = "二联律总数")]
public string TotalSVEBig
{
get { return totalSVEBig; }
set { totalSVEBig = value; }
} //总计
private string sumSVEBig = null;
[XmlElement(ElementName = "总计"")]
public string SumSVEBig
{
get { return sumSVEBig; }
set { sumSVEBig = value; }
} //三联律总数
private string totalSVETrig = null;
[XmlElement(ElementName = "三联律总数")]
public string TotalSVETrig
{
get { return totalSVETrig; }
set { totalSVETrig = value; }
} //总计
private string sumSVETrig = null;
[XmlElement(ElementName = "总计")]
public string SumSVETrig
{
get { return sumSVETrig; }
set { sumSVETrig = value; }
} //一分最大室上性数
private string maxMinuteSVECount = null;
[XmlElement(ElementName = "一分最大室上性数")]
public string MaxMinuteSVECount
{
get { return maxMinuteSVECount; }
set { maxMinuteSVECount = value; }
}
}
}
<室上性早搏>
<总计>123</总计>
<总计>456</总计>
</室上性早搏>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(str);
XmlNodeList node = xml.SelectNodes("/室上性早搏/总计");
for (int i = 0; i < node.Count; i++)
{
(node[i] as XmlElement).SetAttribute("xmlns", "http://www.xxxx.com/" + i);
}
str = xml.OuterXml;
XmlSerializer xs = new XmlSerializer(typeof(VEXml));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str));
VEXml vexml = xs.Deserialize(ms) as VEXml;
Console.WriteLine(vexml.ID + "==" + vexml.SumVETrig + "==" + vexml.SumVEBig); [XmlRoot(ElementName = "室上性早搏")]
public class VEXml
{
private int iD;
[XmlElement(ElementName = "ID")]
public int ID
{
get { return iD; }
set { iD = value; }
}
private string sumVEBig = null;
[XmlElement(ElementName = "总计", Namespace = "http://www.xxxx.com/0")]
public string SumVEBig
{
get { return sumVEBig; }
set { sumVEBig = value; }
}
private string sumVETrig = null;
[XmlElement(ElementName = "总计", Namespace = "http://www.xxxx.com/1")]
public string SumVETrig
{
get { return sumVETrig; }
set { sumVETrig = value; }
}
}
string str = @"<?xml version=""1.0"" encoding=""utf-8""?>
<室上性早搏>
<总计>123</总计>
<总计>456</总计>
</室上性早搏>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(str);
XmlNodeList node = xml.SelectNodes("/室上性早搏/总计");
for (int i = 0; i < node.Count; i++)
{
(node[i] as XmlElement).SetAttribute("xmlns", "urn:SumVEBig" + i);
}
str = xml.OuterXml;
XmlSerializer xs = new XmlSerializer(typeof(VEXml));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str));
VEXml vexml = xs.Deserialize(ms) as VEXml;
Console.WriteLine(vexml.ID + "==" + vexml.SumVETrig + "==" + vexml.SumVEBig);
public class VEXml
{
private int iD;
[XmlElement(ElementName = "ID")]
public int ID
{
get { return iD; }
set { iD = value; }
}
private string sumVEBig = null;
[XmlElement(ElementName = "总计", Namespace = "urn:SumVEBig0")]
public string SumVEBig
{
get { return sumVEBig; }
set { sumVEBig = value; }
}
private string sumVETrig = null;
[XmlElement(ElementName = "总计", Namespace = "urn:SumVEBig1")]
public string SumVETrig
{
get { return sumVETrig; }
set { sumVETrig = value; }
}
}