WCF,XML 反序列化
WCF, XML deserialization
我有一个简单的 WCF 服务
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(ShoppingCart value);
}
public class ShoppingCart
{
public string name;
[XmlElement("ShoppingCartItem")]
public ShoppingCartItem[] ShoppingCartItems;
}
public class ShoppingCartItem
{
public string Description;
}
还有一个简单的测试控制台。当我将服务引用添加到测试控制台时,My ShoppingCart class 进入 TestConsole reference.cs 文件。所以我可以在 TestConsole 项目上这样写:
static void Main(string[] args)
{
ShoppingCart body = new ShoppingCart();
FileStream myFileStream = new FileStream(@"C:\Resources\Xmls\New Folder\shoppingCart.xml", FileMode.Open);
XmlSerializer mySerializer = new XmlSerializer(typeof(ShoppingCart));
ShoppingCart cart = (ShoppingCart)mySerializer.Deserialize(myFileStream);
}
我的shoppingCart.xml文件是这样的:
<?xml version="1.0"?>
<ShoppingCart>
<name>test</name>
<ShoppingCartItem>
<Description>XBox 360</Description>
</ShoppingCartItem>
<ShoppingCartItem>
<Description>Cell Phone</Description>
</ShoppingCartItem>
</ShoppingCart>
所以,我正在等待代码出现
ShoppingCart cart = (ShoppingCart)mySerializer.Deserialize(myFileStream);
行,应该有两项。但是好像是这样的:
你能解释一下为什么我的列表没有填充吗?
您需要像这样添加 Xml 元素属性:
public class ShoppingCart
{
[XmlElement("ShoppingCartItem")]
public ShoppingCartItem[] ShoppingCartItems;
}
如果可以将 Xml 文件更改为此结构并删除 XmlElement 属性,那是一个替代
<ShoppingCart>
<name>test</name>
<ShoppingCartItems>
<ShoppingCartItem>
<Description>XBox 360</Description>
</ShoppingCartItem>
<ShoppingCartItem>
<Description>Cell Phone</Description>
</ShoppingCartItem>
</ShoppingCartItems>
</ShoppingCart>
我有一个简单的 WCF 服务
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(ShoppingCart value);
}
public class ShoppingCart
{
public string name;
[XmlElement("ShoppingCartItem")]
public ShoppingCartItem[] ShoppingCartItems;
}
public class ShoppingCartItem
{
public string Description;
}
还有一个简单的测试控制台。当我将服务引用添加到测试控制台时,My ShoppingCart class 进入 TestConsole reference.cs 文件。所以我可以在 TestConsole 项目上这样写:
static void Main(string[] args)
{
ShoppingCart body = new ShoppingCart();
FileStream myFileStream = new FileStream(@"C:\Resources\Xmls\New Folder\shoppingCart.xml", FileMode.Open);
XmlSerializer mySerializer = new XmlSerializer(typeof(ShoppingCart));
ShoppingCart cart = (ShoppingCart)mySerializer.Deserialize(myFileStream);
}
我的shoppingCart.xml文件是这样的:
<?xml version="1.0"?>
<ShoppingCart>
<name>test</name>
<ShoppingCartItem>
<Description>XBox 360</Description>
</ShoppingCartItem>
<ShoppingCartItem>
<Description>Cell Phone</Description>
</ShoppingCartItem>
</ShoppingCart>
所以,我正在等待代码出现
ShoppingCart cart = (ShoppingCart)mySerializer.Deserialize(myFileStream);
行,应该有两项。但是好像是这样的:
你能解释一下为什么我的列表没有填充吗?
您需要像这样添加 Xml 元素属性:
public class ShoppingCart
{
[XmlElement("ShoppingCartItem")]
public ShoppingCartItem[] ShoppingCartItems;
}
如果可以将 Xml 文件更改为此结构并删除 XmlElement 属性,那是一个替代
<ShoppingCart>
<name>test</name>
<ShoppingCartItems>
<ShoppingCartItem>
<Description>XBox 360</Description>
</ShoppingCartItem>
<ShoppingCartItem>
<Description>Cell Phone</Description>
</ShoppingCartItem>
</ShoppingCartItems>
</ShoppingCart>