C# 如何在xml中发送整型数据
我正在创建XML来发送数据。数据包含多种数据类型,如字符串、整数和十进制。我的XML格式和创建它的c#代码如下C# 如何在xml中发送整型数据,c#,xml,C#,Xml,我正在创建XML来发送数据。数据包含多种数据类型,如字符串、整数和十进制。我的XML格式和创建它的c#代码如下 <root> <data> <struct> <PartnerCD></PartnerCD> <UserName> </UserName> <Password> </Pas
<root>
<data>
<struct>
<PartnerCD></PartnerCD>
<UserName> </UserName>
<Password> </Password>
<Action> </Action>
<OfficeCD></OfficeCD>
<ChannelCD></ChannelCD>
<Token></Token>
<Notes> </Notes>
<Products>
<Product>
<ProductID></ProductID>
<SVA></SVA>
<Amount></Amount>
</Product>
</Products>
</struct>
</data>
</root>
我得到的结果是
<root>
<data>
<struct>
<PartnerCD>123</PartnerCD>
<UserName>api</UserName>
<Password>pass</Password>
<Action>token</Action>
<Token>4847898</Token>
<Products>
<Product>
<ProductID>123</ProductID>
<SVA>e8a8227c-bba3-4f32-a2cd-15e8f246344b</SVA>
<Amount>700</Amount>
</Product>
</Products>
</struct>
</data>
</root>
123
应用程序编程接口
通过
代币
4847898
123
e8a8227c-bba3-4f32-a2cd-15e8f246344b
700
我希望PartnerCD和ProductId元素为整数,Amount元素为十进制。我试过XMLNodeType,但没有用。Xml本身没有“数据类型”的概念,您所指的
XMLNodeType
是它是什么类型的节点(如元素、属性等),而不是其中包含什么类型的数据。数据类型在Xml中是不相关的,它都是在它使用的模式中定义的,这是存储预期数据类型声明的位置
因此,如果您有一个XSD,其中指出
/root/data/struct/PartnerCD
的类型为xs:int
,那么在验证文件时将出现验证错误。XML本身只是所有数据的容器,它不包含元数据。您可以手动定义它,但在大约99.99%的情况下,我无法理解这一点,它或多或少是XML的杂种,就像MSXML一样,可以理解您在做什么。个人而言,我会创建一个对象并从XML中序列化\反序列化为\像这样
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
namespace _37321906
{
class Program
{
static void Main(string[] args)
{
Root root = new Root();
root.Data.Struct.PartnerCD = 123;
root.Data.Struct.UserName = "api";
root.Data.Struct.Password = "pass";
root.Data.Struct.Action = "token";
root.Data.Struct.Token = 4847898;
root.Data.Struct.Products.Product.Add(new Product { ProductID = 123, SVA = "e8a8227c-bba3-4f32-a2cd-15e8f246344b", Amount = 700.0001 });
// Serialize the root object to XML
Serialize<Root>(root);
// Deserialize from XML
Root DeserializeRoot = Deserialize<Root>();
}
private static void Serialize<T>(T data)
{
// Use a file stream here.
using (TextWriter WriteFileStream = new StreamWriter("test.xml"))
{
// Construct a SoapFormatter and use it
// to serialize the data to the stream.
XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
try
{
// Serialize EmployeeList to the file stream
SerializerObj.Serialize(WriteFileStream, data);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
}
}
}
private static T Deserialize<T>() where T : new()
{
//List<Employee> EmployeeList2 = new List<Employee>();
// Create an instance of T
T ReturnListOfT = CreateInstance<T>();
// Create a new file stream for reading the XML file
using (FileStream ReadFileStream = new FileStream("test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Construct a XmlSerializer and use it
// to serialize the data from the stream.
XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
try
{
// Deserialize the hashtable from the file
ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
}
}
// return the Deserialized data.
return ReturnListOfT;
}
// function to create instance of T
public static T CreateInstance<T>() where T : new()
{
return (T)Activator.CreateInstance(typeof(T));
}
}
[XmlRoot(ElementName = "Product")]
public class Product
{
[XmlElement(ElementName = "ProductID")]
public int ProductID { get; set; }
[XmlElement(ElementName = "SVA")]
public string SVA { get; set; }
[XmlElement(ElementName = "Amount")]
public double Amount { get; set; }
}
[XmlRoot(ElementName = "Products")]
public class Products
{
public Products()
{
this.Product = new List<Product>();
}
[XmlElement(ElementName = "Product")]
public List<Product> Product { get; set; }
}
[XmlRoot(ElementName = "struct")]
public class Struct
{
public Struct()
{
this.Products = new Products();
}
[XmlElement(ElementName = "PartnerCD")]
public int PartnerCD { get; set; }
[XmlElement(ElementName = "UserName")]
public string UserName { get; set; }
[XmlElement(ElementName = "Password")]
public string Password { get; set; }
[XmlElement(ElementName = "Action")]
public string Action { get; set; }
[XmlElement(ElementName = "OfficeCD")]
public string OfficeCD { get; set; }
[XmlElement(ElementName = "ChannelCD")]
public string ChannelCD { get; set; }
[XmlElement(ElementName = "Token")]
public int Token { get; set; }
[XmlElement(ElementName = "Notes")]
public string Notes { get; set; }
[XmlElement(ElementName = "Products")]
public Products Products { get; set; }
}
[XmlRoot(ElementName = "data")]
public class Data
{
public Data()
{
this.Struct = new Struct();
}
[XmlElement(ElementName = "struct")]
public Struct Struct { get; set; }
}
[XmlRoot(ElementName = "root")]
public class Root
{
public Root()
{
this.Data = new Data();
}
[XmlElement(ElementName = "data")]
public Data Data { get; set; }
}
}
使用系统;
使用System.Xml.Serialization;
使用System.Collections.Generic;
使用System.IO;
名称空间_37321906
{
班级计划
{
静态void Main(字符串[]参数)
{
根=新根();
root.Data.Struct.PartnerCD=123;
root.Data.Struct.UserName=“api”;
root.Data.Struct.Password=“通过”;
root.Data.Struct.Action=“令牌”;
root.Data.Struct.Token=4847898;
root.Data.Struct.Products.Product.Add(新产品{ProductID=123,SVA=“e8a8227c-bba3-4f32-a2cd-15e8f246344b”,金额=700.0001});
//将根对象序列化为XML
序列化(根);
//从XML反序列化
根反序列化zeroot=反序列化();
}
私有静态void序列化(T数据)
{
//在这里使用文件流。
使用(TextWriter WriteFileStream=new StreamWriter(“test.xml”))
{
//构造一个SoapFormatter并使用它
//将数据序列化到流。
XmlSerializer SerializerObj=新的XmlSerializer(typeof(T));
尝试
{
//将EmployeeList序列化到文件流
SerializerObj.Serialize(WriteFileStream,数据);
}
捕获(例外情况除外)
{
WriteLine(string.Format(“序列化失败。原因:{0}”,ex.Message));
}
}
}
私有静态T反序列化(),其中T:new()
{
//List EmployeeList2=新列表();
//创建一个T的实例
T returnListSoft=CreateInstance();
//创建用于读取XML文件的新文件流
使用(FileStream ReadFileStream=newfilestream(“test.xml”、FileMode.Open、FileAccess.Read、FileShare.Read))
{
//构造一个XmlSerializer并使用它
//从流中序列化数据。
XmlSerializer SerializerObj=新的XmlSerializer(typeof(T));
尝试
{
//从文件中反序列化哈希表
returnListSoft=(T)SerializerObj.Deserialize(ReadFileStream);
}
捕获(例外情况除外)
{
WriteLine(string.Format(“序列化失败。原因:{0}”,ex.Message));
}
}
//返回反序列化的数据。
ReturnListOfT;
}
//函数创建T的实例
公共静态T CreateInstance(),其中T:new()
{
return(T)Activator.CreateInstance(typeof(T));
}
}
[XmlRoot(ElementName=“产品”)]
公共类产品
{
[xmlement(ElementName=“ProductID”)]
public int ProductID{get;set;}
[xmlement(ElementName=“SVA”)]
公共字符串SVA{get;set;}
[xmlement(ElementName=“Amount”)]
公共双倍金额{get;set;}
}
[XmlRoot(ElementName=“Products”)]
公共类产品
{
公共产品()
{
this.Product=新列表();
}
[XmlElement(ElementName=“产品”)]
公共列表产品{get;set;}
}
[XmlRoot(ElementName=“struct”)]
公共类结构
{
公共结构()
{
this.Products=新产品();
}
[xmlement(ElementName=“PartnerCD”)]
public int PartnerCD{get;set;}
[xmlement(ElementName=“UserName”)]
公共字符串用户名{get;set;}
[xmlement(ElementName=“Password”)]
公共字符串密码{get;set;}
[xmlement(ElementName=“Action”)]
公共字符串操作{get;set;}
[xmlement(ElementName=“OfficeCD”)]
公共字符串OfficeCD{get;set;}
[xmlement(ElementName=“ChannelCD”)]
公共字符串ChannelCD{get;set;}
[xmlement(ElementName=“Token”)]
公共int标记{get;set;}
[xmlement(ElementName=“Notes”)]
公共字符串注释{get;set;}
[xmlement(ElementName=“Products”)]
公共产品产品{get;set;}
}
[XmlRoot(ElementName=“data”)]
公共类数据
{
公共数据()
{
this.Struct=new Struct();
}
[xml元素(El)
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
namespace _37321906
{
class Program
{
static void Main(string[] args)
{
Root root = new Root();
root.Data.Struct.PartnerCD = 123;
root.Data.Struct.UserName = "api";
root.Data.Struct.Password = "pass";
root.Data.Struct.Action = "token";
root.Data.Struct.Token = 4847898;
root.Data.Struct.Products.Product.Add(new Product { ProductID = 123, SVA = "e8a8227c-bba3-4f32-a2cd-15e8f246344b", Amount = 700.0001 });
// Serialize the root object to XML
Serialize<Root>(root);
// Deserialize from XML
Root DeserializeRoot = Deserialize<Root>();
}
private static void Serialize<T>(T data)
{
// Use a file stream here.
using (TextWriter WriteFileStream = new StreamWriter("test.xml"))
{
// Construct a SoapFormatter and use it
// to serialize the data to the stream.
XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
try
{
// Serialize EmployeeList to the file stream
SerializerObj.Serialize(WriteFileStream, data);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
}
}
}
private static T Deserialize<T>() where T : new()
{
//List<Employee> EmployeeList2 = new List<Employee>();
// Create an instance of T
T ReturnListOfT = CreateInstance<T>();
// Create a new file stream for reading the XML file
using (FileStream ReadFileStream = new FileStream("test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Construct a XmlSerializer and use it
// to serialize the data from the stream.
XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
try
{
// Deserialize the hashtable from the file
ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
}
}
// return the Deserialized data.
return ReturnListOfT;
}
// function to create instance of T
public static T CreateInstance<T>() where T : new()
{
return (T)Activator.CreateInstance(typeof(T));
}
}
[XmlRoot(ElementName = "Product")]
public class Product
{
[XmlElement(ElementName = "ProductID")]
public int ProductID { get; set; }
[XmlElement(ElementName = "SVA")]
public string SVA { get; set; }
[XmlElement(ElementName = "Amount")]
public double Amount { get; set; }
}
[XmlRoot(ElementName = "Products")]
public class Products
{
public Products()
{
this.Product = new List<Product>();
}
[XmlElement(ElementName = "Product")]
public List<Product> Product { get; set; }
}
[XmlRoot(ElementName = "struct")]
public class Struct
{
public Struct()
{
this.Products = new Products();
}
[XmlElement(ElementName = "PartnerCD")]
public int PartnerCD { get; set; }
[XmlElement(ElementName = "UserName")]
public string UserName { get; set; }
[XmlElement(ElementName = "Password")]
public string Password { get; set; }
[XmlElement(ElementName = "Action")]
public string Action { get; set; }
[XmlElement(ElementName = "OfficeCD")]
public string OfficeCD { get; set; }
[XmlElement(ElementName = "ChannelCD")]
public string ChannelCD { get; set; }
[XmlElement(ElementName = "Token")]
public int Token { get; set; }
[XmlElement(ElementName = "Notes")]
public string Notes { get; set; }
[XmlElement(ElementName = "Products")]
public Products Products { get; set; }
}
[XmlRoot(ElementName = "data")]
public class Data
{
public Data()
{
this.Struct = new Struct();
}
[XmlElement(ElementName = "struct")]
public Struct Struct { get; set; }
}
[XmlRoot(ElementName = "root")]
public class Root
{
public Root()
{
this.Data = new Data();
}
[XmlElement(ElementName = "data")]
public Data Data { get; set; }
}
}