Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#类到XML(XMLSerialization)问题_C#_Xml - Fatal编程技术网

C#类到XML(XMLSerialization)问题

C#类到XML(XMLSerialization)问题,c#,xml,C#,Xml,目前我正在使用xml,必须从C#对象序列化填充xml文件 <DVD> <Starring> <Star position="actor"> Tom Hanks </Star> <Star position="actress"> Robin Wright </Star> <Title>Forrest Gump</Title> </Sta

目前我正在使用xml,必须从C#对象序列化填充xml文件

<DVD>
<Starring>
    <Star position="actor">
        Tom Hanks
    </Star>
   <Star position="actress">
        Robin Wright
    </Star>
<Title>Forrest Gump</Title>
</Starring>
</DVD>

汤姆汉克斯
罗宾赖特
阿甘正传
我有两门课:DVD和Star

Public class DVD
{
  string title;
  public string Title{get{return title;}set {title=value;}}
  List<Star> Starring=new List<Star>();
}
public class Star
{
   string star;
   string pos;
   [XmlAttribute]
   public string Position{get{return pos;}set{pos=value;}}
   //Actually i don't want this property as a xmlelement
    public String StarName
   {
     get{return star;}
     set {star=value;} 
   }
}
公共类DVD
{
字符串标题;
公共字符串标题{get{return Title;}set{Title=value;}}
列表加星号=新列表();
}
公务舱明星
{
弦星;
字符串位置;
[XmlAttribute]
公共字符串位置{get{return pos;}set{pos=value;}}
//实际上,我不希望将此属性作为xmlelement
公共字符串星名
{
获取{返回星;}
设置{star=value;}
}
}
序列化的结果是

<DVD>
<Starring>
    <Star position="actor">
        <StarName>Tom Hanks</StarName>
    </Star>
   <Star position="actress">
        <StarName>Robin Wright</StarName>
    </Star>
<Title>Forrest Gump</Title>
</Starring>
</DVD>

汤姆汉克斯
罗宾赖特
阿甘正传

我的问题是,我找不到在没有StarName节点的情况下如何包装演员和演员姓名的方法。有没有办法做到这一点?如果有任何想法,我将不胜感激。

在StarName属性上使用XmlTextAttribute。

我不相信你!给定您的代码,Title元素将位于starting元素之外——实际上是该元素的对等元素。