C# XML反序列化成对象 子元素可变该何解?求大神见教
C# XML反序列化成对象 子元素可变该何解????求大神指教
XML节构如下:W节点下会有子元素w1-wn n不固定 我现在需要反序列化这个XML成对象该何解?????求大神指教
<w id="218070">
<w1 h="考登比斯" a="哈茨"/>
<w2 h="克罗托内" a="弗洛西诺尼"/>
<w3 h="克罗托内" a="弗洛西诺尼"/>
<w4 h="克罗托内" a="弗洛西诺尼"/>
<w5 h="克罗托内" a="弗洛西诺尼"/>
........
</w>
以下是方便调试代码:
[XmlRoot("w")]
public class Match
{
[XmlAttribute("id")]
public long Id;
//以下不知道该怎么写
[XmlElement("w1")]
public MatchInfo W1;
}
public class MatchInfo
{
[XmlAttribute("h")]
public string HostName;
[XmlAttribute("a")]
public string AwayName;
}
C#反序列化代码如下:
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Match));
Match resultMatch = null;
using (System.IO.Stream stream = new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(content)))
{
resultMatch = serializer.Deserialize(stream) as Match;
}
------解决思路----------------------
百度,linq to xml
select new {
字段=值
}
XML节构如下:W节点下会有子元素w1-wn n不固定 我现在需要反序列化这个XML成对象该何解?????求大神指教
<w id="218070">
<w1 h="考登比斯" a="哈茨"/>
<w2 h="克罗托内" a="弗洛西诺尼"/>
<w3 h="克罗托内" a="弗洛西诺尼"/>
<w4 h="克罗托内" a="弗洛西诺尼"/>
<w5 h="克罗托内" a="弗洛西诺尼"/>
........
</w>
以下是方便调试代码:
[XmlRoot("w")]
public class Match
{
[XmlAttribute("id")]
public long Id;
//以下不知道该怎么写
[XmlElement("w1")]
public MatchInfo W1;
}
public class MatchInfo
{
[XmlAttribute("h")]
public string HostName;
[XmlAttribute("a")]
public string AwayName;
}
C#反序列化代码如下:
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Match));
Match resultMatch = null;
using (System.IO.Stream stream = new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(content)))
{
resultMatch = serializer.Deserialize(stream) as Match;
}
------解决思路----------------------
百度,linq to xml
select new {
字段=值
}