Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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序列化的XmlRoot()不起作用_C#_Xml Serialization_Xmlroot - Fatal编程技术网

C# 用于Xml序列化的XmlRoot()不起作用

C# 用于Xml序列化的XmlRoot()不起作用,c#,xml-serialization,xmlroot,C#,Xml Serialization,Xmlroot,我正在尝试让我的httphandler打印一个XML文件,格式如下: <ScheduledShows> <ScheduledShowElement>...</ScheduledShowElement> <ScheduledShowElement>...</ScheduledShowElement> <ScheduledShowElement>...</ScheduledShowElement&g

我正在尝试让我的httphandler打印一个XML文件,格式如下:

<ScheduledShows>
    <ScheduledShowElement>...</ScheduledShowElement>
    <ScheduledShowElement>...</ScheduledShowElement>
    <ScheduledShowElement>...</ScheduledShowElement>
</ScheduledShows>
xml文件的输出

 <?xml version="1.0" encoding="utf-8"?>
<ScheduledShows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T10:20:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666100</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">When Dooley demonstrates his newest hobby, magic, Willa gets an idea.  A huge backyard magic show!  Starring the Great Doolini and his amazing disappearing elephant trick!  Unfortunately, Lou the elephant misunderstands and thinks Willa wants him to disappear for real.   In the middle of the show Willa must find Lou and apologize so he&amp;rsquo;ll reappear! / When the animals and Willa discover an egg in their backyard, their parental propriety  kicks in.   Especially when Dooley relates a factoid about young hatchlings imprinting on the first critter they see.   Willa&amp;rsquo;s critters vie for egg watching rights, so they can be first to be called Mama!  Or Poppa!</StoryLine>
        <Title xmlns="http://example.books.com">Disappearing Act / Great Eggspectations</Title>
        <ProgramId xmlns="http://example.books.com">2202</ProgramId>
        <Genre xmlns="http://example.books.com">Animated</Genre>
        <ProgramName xmlns="http://example.books.com">Willa's Wild Life</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">68914</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com">willas_wildlife</ShowUrl>
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T10:45:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666105</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">It&amp;rsquo;s Club Day in Gloomsville. The gang splinter off to form clubs and prepare for the club&amp;rsquo;s appearance in the big Gloomsville parade.  Skull Boy forms the coolest club of all with some new jazzy skeletal friends that no one ever sees.  At first no one believes Skull Boy has these new friends since every time they want to meet them, they disappear.   When pressed for details, she admits she hasn&amp;rsquo;t met them yet &amp;ndash; they&amp;rsquo;re imaginary. </StoryLine>
        <Title xmlns="http://example.books.com">Skull Boys Don't Cry</Title>
        <ProgramId xmlns="http://example.books.com">1418</ProgramId>
        <Genre xmlns="http://example.books.com">Animated</Genre>
        <ProgramName xmlns="http://example.books.com">Ruby Gloom</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">54297</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com">rubygloom</ShowUrl>
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T11:10:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666113</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">When Mad Margaret gets trapped in a jar she becomes a source of entertainment for Erky./Erky and Perky need a place to live and who better to find it for them than Frenzel.</StoryLine>
        <Title xmlns="http://example.books.com">Erky's Birthday / Location Location Location</Title>
        <ProgramId xmlns="http://example.books.com">1347</ProgramId>
        <Genre xmlns="http://example.books.com">Animated</Genre>
        <ProgramName xmlns="http://example.books.com">Erky Perky</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">49009</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com">erky_perky</ShowUrl>
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T11:35:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666116</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">SYNOPSIS:The Joyco toy company has heard about George and his Zoopercar and they want to market it as their newest toy.  But George isn't interested in selling his prized mode of transportation.  Afterall, he and his Dad built it together and no one can take that special bond away from him.  But two Joyco toy employees, Barry and Steve, have other plans.  If George won't sell it to them, they will just have to take it, which they do.  But once their boss, Big Ed Easy finds out that George has be</StoryLine>
        <Title xmlns="http://example.books.com">ZOOPERCAR CAPER</Title>
        <ProgramId xmlns="http://example.books.com">311</ProgramId>
        <Genre xmlns="http://example.books.com" />
        <ProgramName xmlns="http://example.books.com">George Shrinks</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">25371</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com" />
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T11:35:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666116</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">SYNOPSIS:The Joyco toy company has heard about George and his Zoopercar and they want to market it as their newest toy.  But George isn't interested in selling his prized mode of transportation.  Afterall, he and his Dad built it together and no one can take that special bond away from him.  But two Joyco toy employees, Barry and Steve, have other plans.  If George won't sell it to them, they will just have to take it, which they do.  But once their boss, Big Ed Easy finds out that George has be</StoryLine>
        <Title xmlns="http://example.books.com">ZOOPERCAR CAPER</Title>
        <ProgramId xmlns="http://example.books.com">311</ProgramId>
        <Genre xmlns="http://example.books.com" />
        <ProgramName xmlns="http://example.books.com">George Shrinks</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">25371</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com">george_shrinks</ShowUrl>
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
</ScheduledShows>

2009-09-17T10:20:00
770
17666100
当杜利展示他的最新爱好——魔术时,威拉有了一个主意。一场巨大的后院魔术表演!主演伟大的杜里尼和他惊人的消失大象把戏!不幸的是,大象卢误解了这一点,认为威拉真的想让他消失。在节目的中间,Willa必须找到娄并向他道歉。rsquo;我会再出现的!/当动物们和威拉在他们的后院发现一个鸡蛋时,他们父母的礼节就开始生效了。尤其是当杜利讲述了一个关于幼仔在他们看到的第一只动物身上留下印记的故事。威拉;rsquo;美国的小动物争抢看蛋权,所以他们可以第一个被称为妈妈!或者爸爸!
消失的行为/伟大的幻想
2202
有生气的
薇拉的野生生活
68914
野生动物协会
没有一个
2009-09-17T10:45:00
770
17666105
资讯科技及;rsquo;在乌姆斯维尔的俱乐部日。这伙人四分五裂,组成俱乐部,为俱乐部做准备;rsquo;她在大灰熊镇游行中露面。骷髅男孩与一些没有人见过的新的爵士骨架朋友组成了最酷的俱乐部。起初没人相信骷髅男孩有了这些新朋友,因为每次他们想见他们时,他们就消失了。当被追问到细节时,她承认自己没有;rsquo;我还没见过他们&;恩达什;他们,;rsquo;你是虚构的。
骷髅男孩不哭
1418
有生气的
露比的怪诞城
54297
橡胶织机
没有一个
2009-09-17T11:10:00
770
17666113
当疯狂的玛格丽特被困在罐子里时,她成了埃尔基的娱乐来源。/埃尔基和珀基需要一个居住的地方,谁能比弗伦泽尔更好地为他们找到这个地方。
Erky的生日/地点
1347
有生气的
倒霉虫虫
49009
erky_perky
没有一个
2009-09-17T11:35:00
770
17666116
简介:Joyco玩具公司听说了乔治和他的Zoopercar,他们想把它作为最新的玩具推向市场。但乔治对出售他珍视的交通工具不感兴趣。毕竟,他和他爸爸一起建造了它,没有人能从他身上夺走这种特殊的纽带。但Joyco玩具公司的两名员工Barry和Steve还有其他计划。如果乔治不把它卖给他们,他们就只能接受它,他们就是这样做的。但一旦他们的老板,大埃德易发现乔治已经被解雇了
游隼
311
乔治退缩了
25371
没有一个
2009-09-17T11:35:00
770
17666116
简介:Joyco玩具公司听说了乔治和他的Zoopercar,他们想把它作为最新的玩具推向市场。但乔治对出售他珍视的交通工具不感兴趣。毕竟,他和他爸爸一起建造了它,没有人能从他身上夺走这种特殊的纽带。但Joyco玩具公司的两名员工Barry和Steve还有其他计划。如果乔治不把它卖给他们,他们就只能接受它,他们就是这样做的。但一旦他们的老板,大埃德易发现乔治已经被解雇了
游隼
311
乔治退缩了
25371
乔治·休斯
没有一个
[XmlRoot(…)]
只影响最外层的元素(
)。我怀疑您想要
[xmlement(…)]
。当然,另一种方法是编写对象包装器:

[XmlRoot("SheduledShows")]
public class Shows {
    [XmlElement("SheduledShowElement")]
    public List<Show> Shows {get;set;}
}
[XmlRoot(“ScheduledShows”)]
公开课{
[XmlElement(“ScheduledShowElement”)]
公共列表显示{get;set;}
}

并序列化此包装器对象,而不仅仅是列表。

您需要使用重载构造函数,该构造函数接受
XmlAttributeOverrides
,并确保覆盖
ScheduledShow
类的名称。

我在这里找到了问题的答案:

我刚刚使用了
[XmlType(“”)
,这很有效

using System;
using System.Xml.Serialization;


namespace CommunityServer.Scheduler
{

    [XmlType("ScheduledShowElement")]
    public class ScheduledShow
    {

      ...
    }
}

好极了这件事让我抓狂。
 <?xml version="1.0" encoding="utf-8"?>
<ScheduledShows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T10:20:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666100</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">When Dooley demonstrates his newest hobby, magic, Willa gets an idea.  A huge backyard magic show!  Starring the Great Doolini and his amazing disappearing elephant trick!  Unfortunately, Lou the elephant misunderstands and thinks Willa wants him to disappear for real.   In the middle of the show Willa must find Lou and apologize so he&amp;rsquo;ll reappear! / When the animals and Willa discover an egg in their backyard, their parental propriety  kicks in.   Especially when Dooley relates a factoid about young hatchlings imprinting on the first critter they see.   Willa&amp;rsquo;s critters vie for egg watching rights, so they can be first to be called Mama!  Or Poppa!</StoryLine>
        <Title xmlns="http://example.books.com">Disappearing Act / Great Eggspectations</Title>
        <ProgramId xmlns="http://example.books.com">2202</ProgramId>
        <Genre xmlns="http://example.books.com">Animated</Genre>
        <ProgramName xmlns="http://example.books.com">Willa's Wild Life</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">68914</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com">willas_wildlife</ShowUrl>
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T10:45:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666105</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">It&amp;rsquo;s Club Day in Gloomsville. The gang splinter off to form clubs and prepare for the club&amp;rsquo;s appearance in the big Gloomsville parade.  Skull Boy forms the coolest club of all with some new jazzy skeletal friends that no one ever sees.  At first no one believes Skull Boy has these new friends since every time they want to meet them, they disappear.   When pressed for details, she admits she hasn&amp;rsquo;t met them yet &amp;ndash; they&amp;rsquo;re imaginary. </StoryLine>
        <Title xmlns="http://example.books.com">Skull Boys Don't Cry</Title>
        <ProgramId xmlns="http://example.books.com">1418</ProgramId>
        <Genre xmlns="http://example.books.com">Animated</Genre>
        <ProgramName xmlns="http://example.books.com">Ruby Gloom</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">54297</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com">rubygloom</ShowUrl>
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T11:10:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666113</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">When Mad Margaret gets trapped in a jar she becomes a source of entertainment for Erky./Erky and Perky need a place to live and who better to find it for them than Frenzel.</StoryLine>
        <Title xmlns="http://example.books.com">Erky's Birthday / Location Location Location</Title>
        <ProgramId xmlns="http://example.books.com">1347</ProgramId>
        <Genre xmlns="http://example.books.com">Animated</Genre>
        <ProgramName xmlns="http://example.books.com">Erky Perky</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">49009</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com">erky_perky</ShowUrl>
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T11:35:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666116</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">SYNOPSIS:The Joyco toy company has heard about George and his Zoopercar and they want to market it as their newest toy.  But George isn't interested in selling his prized mode of transportation.  Afterall, he and his Dad built it together and no one can take that special bond away from him.  But two Joyco toy employees, Barry and Steve, have other plans.  If George won't sell it to them, they will just have to take it, which they do.  But once their boss, Big Ed Easy finds out that George has be</StoryLine>
        <Title xmlns="http://example.books.com">ZOOPERCAR CAPER</Title>
        <ProgramId xmlns="http://example.books.com">311</ProgramId>
        <Genre xmlns="http://example.books.com" />
        <ProgramName xmlns="http://example.books.com">George Shrinks</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">25371</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com" />
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
    <ScheduledShow>
        <AirTime xmlns="http://example.books.com">2009-09-17T11:35:00</AirTime>
        <StationId xmlns="http://example.books.com">770</StationId>
        <ScheduleRowId xmlns="http://example.books.com">17666116</ScheduleRowId>
        <StoryLine xmlns="http://example.books.com">SYNOPSIS:The Joyco toy company has heard about George and his Zoopercar and they want to market it as their newest toy.  But George isn't interested in selling his prized mode of transportation.  Afterall, he and his Dad built it together and no one can take that special bond away from him.  But two Joyco toy employees, Barry and Steve, have other plans.  If George won't sell it to them, they will just have to take it, which they do.  But once their boss, Big Ed Easy finds out that George has be</StoryLine>
        <Title xmlns="http://example.books.com">ZOOPERCAR CAPER</Title>
        <ProgramId xmlns="http://example.books.com">311</ProgramId>
        <Genre xmlns="http://example.books.com" />
        <ProgramName xmlns="http://example.books.com">George Shrinks</ProgramName>
        <SimsTitleId xmlns="http://example.books.com">25371</SimsTitleId>
        <ShowUrl xmlns="http://example.books.com">george_shrinks</ShowUrl>
        <CssClass xmlns="http://example.books.com">none</CssClass>
    </ScheduledShow>
</ScheduledShows>
[XmlRoot("SheduledShows")]
public class Shows {
    [XmlElement("SheduledShowElement")]
    public List<Show> Shows {get;set;}
}
using System;
using System.Xml.Serialization;


namespace CommunityServer.Scheduler
{

    [XmlType("ScheduledShowElement")]
    public class ScheduledShow
    {

      ...
    }
}