[C#]记一次解析XML转对象的笔记
项目中调用第三方API,返回格式是XML字符串,需要将XML反序列化为对象,格式如下:
<?xml version="1.0"?>
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" totalCount="" resultCode="ABC" resultMessage="测试">
<profile lastName="李" firstName="爽" dateOfBirth="XX0816" age="" gender="" address="浙江省 杭州富阳市 东州工业园区3号路1号杭州龙盛工贸有限公" email="htlslll@163.com" cellEmail="" socialMedia="" homePhone="" cellPhone="" contactabiltyMail="" contactabiltyEmail="" contactabiltyCellEmail="" contactabiltyHomePhone="" contactabiltyCellPhone="" contactabiltyText="" contactabiltySocialMedia="" vipLevel="" memberId="OC540C00016824" saAccount="" classa="小姐" homeStoreCode="OC54" />
<purchaseHistory>
<departmentPurchase totalLifeTimeSale="" totalTYAmount="" lastTxn="" totalTYUnitCnt="" firstTxn="">
<department categoryName="D08" tyAmount="" tyUnitCnt="" />
<department categoryName="D01" tyAmount="" tyUnitCnt="" />
<department categoryName="D02" tyAmount="" tyUnitCnt="" />
</departmentPurchase>
<ratePurchase totalLyAmount="" totalLyRate="" totalTyAmount="" totalTyRate="" totalLifeTimeAmount="" totalLifeTimeRate="">
<rate categoryName="Retail" lyAmount="" lyRate="" lyToDate="" tyAmount="" tyRate="" tyToDate="" lifeTimeAmount="" lifeTimeRate="" lyTyChange="" />
</ratePurchase>
<storeRatePurchase>
<storeRate />
</storeRatePurchase>
</purchaseHistory>
<vipStatus issueDate="" issueShop="OC54" ptsToUpgrade="" expiryDate="" upgradeShop="" ptsToRenew="" />
<comments comment="第一场雪" />
</Response>
按照这个格式我需要定义相应的对象来接收,定义如下:
/// <summary>
///
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "Response")]
public class Response
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string totalCount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string resultCode { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string resultMessage { get; set; } [System.Xml.Serialization.XmlElementAttribute("profile", typeof(profile))]
public profile profile { get; set; } [System.Xml.Serialization.XmlElementAttribute("purchaseHistory", typeof(purchaseHistory))]
public purchaseHistory purchaseHistory { get; set; } [System.Xml.Serialization.XmlElementAttribute("vipStatus", typeof(vipStatus))]
public vipStatus vipStatus { get; set; } [System.Xml.Serialization.XmlElementAttribute("comments", typeof(comments))]
public comments comments { get; set; }
} [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class profile
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string lastName { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string firstName { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string dateOfBirth { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string age { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string gender { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string address { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string email { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string cellEmail { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string socialMedia { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string homePhone { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string cellPhone { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string contactabiltyMail { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string contactabiltyEmail { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string contactabiltyCellEmail { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string contactabiltyHomePhone { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string contactabiltyCellPhone { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string contactabiltyText { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string contactabiltySocialMedia { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string vipLevel { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string memberId { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string saAccount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string classa { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string homeStoreCode { get; set; }
} [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class purchaseHistory
{ [System.Xml.Serialization.XmlElementAttribute("departmentPurchase", typeof(departmentPurchase))]
public departmentPurchase departmentPurchase { get; set; } [System.Xml.Serialization.XmlElementAttribute("ratePurchase", typeof(ratePurchase))]
public ratePurchase ratePurchase { get; set; } [System.Xml.Serialization.XmlElementAttribute("storeRatePurchase", typeof(storeRatePurchase))]
public storeRatePurchase storeRatePurchase { get; set; }
} [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class departmentPurchase
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string totalTYAmount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string totalTYUnitCnt { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string firstTxn { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string lastTxn { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string totalLifeTimeSale { get; set; } [System.Xml.Serialization.XmlElementAttribute("department", typeof(List<department>), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = )]
public List<department> department { get; set; }
} [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class department
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string categoryName { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string tyAmount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string tyUnitCnt { get; set; }
} [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class ratePurchase
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string totalLyAmount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string totalLyRate { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string totalTyAmount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string totalTyRate { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string totalLifeTimeAmount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string totalLifeTimeRate { get; set; } [System.Xml.Serialization.XmlElementAttribute("rate", typeof(rate))]
public rate rate { get; set; }
} [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class rate
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string categoryName { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string lyAmount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string lyRate { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string lyToDate { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string tyAmount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string tyRate { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute()] public string tyToDate { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string lifeTimeAmount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string lifeTimeRate { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string lyTyChange { get; set; } } [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class storeRatePurchase
{
public string storeRate { get; set; }
} [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class vipStatus
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string issueDate { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string issueShop { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string ptsToUpgrade { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string expiryDate { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string upgradeShop { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string ptsToRenew { get; set; }
} [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class comments
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string comment { get; set; }
}
解析代码如下
private static System.Xml.Serialization.XmlSerializer Serializer;
/// <summary>
/// 指定XML字符串序列化成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xml"></param>
/// <returns></returns>
public static T DeserializeForXml<T>(string xml) where T : class, new()
{
System.IO.StringReader stringReader = null;
try
{
Serializer = Serializer ?? new XmlSerializer(typeof(T));
stringReader = new StringReader(xml);
T result = Serializer.Deserialize(stringReader) as T;
return result;
}
finally
{
if ((stringReader != null))
{
stringReader.Dispose();
}
}
}
调用方式:
var rpXML = AcxiomXMLHelper.DeserializeForXml<CustomerDetails.Response>(returnXML);
好了,接下来就出现了问题
departmentPurchase 下面的department始终是没有数据,调试好久都找不到问题出现在哪里。
尝试找解决的办法,先实例化一个Response对象序列化后写入文件,再将文件内容读出来反序列化出来
Response response = new Response
{
totalCount = "",
resultCode = "ABC",
resultMessage = "测试",
profile = new profile { lastName = "李", firstName = "爽", dateOfBirth = "XX0816", age = "", gender = "", address = "浙江省 杭州富阳市 东州工业园区3号路1号杭州龙盛工贸有限公", email = "htlslll@163.com", cellEmail = "", socialMedia = "", homePhone = "", cellPhone = "", contactabiltyMail = "", contactabiltyEmail = "", contactabiltyCellEmail = "", contactabiltyHomePhone = "", contactabiltyCellPhone = "", contactabiltyText = "", contactabiltySocialMedia = "", vipLevel = "", memberId = "OC540C00016824", saAccount = "", classa = "小姐", homeStoreCode = "OC54" },
comments = new comments { comment = "第一场雪" },
purchaseHistory = new purchaseHistory
{
departmentPurchase = new departmentPurchase
{
totalTYAmount = "",
totalTYUnitCnt = "",
firstTxn = "",
lastTxn = "",
totalLifeTimeSale = "",
department = new List<department> {
new department { categoryName = "D08", tyAmount = "", tyUnitCnt = "" },
new department { categoryName = "D01", tyAmount = "", tyUnitCnt = "" },
new department { categoryName = "D02", tyAmount = "", tyUnitCnt = "" }
}
},
ratePurchase = new ratePurchase
{
totalLyAmount = "",
totalLyRate = "",
totalTyAmount = "",
totalTyRate = "",
totalLifeTimeAmount = "",
totalLifeTimeRate = "",
rate = new rate { categoryName = "Retail", lyAmount = "", lyRate = "", lyToDate = "", tyAmount = "", tyRate = "", tyToDate = "", lifeTimeAmount = "", lifeTimeRate = "", lyTyChange = "" }
},
storeRatePurchase = new storeRatePurchase { storeRate = string.Empty }
},
vipStatus = new vipStatus { issueDate = "", issueShop = "OC54", ptsToUpgrade = "", expiryDate = "", upgradeShop = "", ptsToRenew = "" }
}; FileStream stream = new FileStream(@"d:\xmlFormat.xml", FileMode.Create);
XmlSerializer xmlserilize = new XmlSerializer(typeof(Response));
xmlserilize.Serialize(stream, response); stream.Close(); using (FileStream xmlStream = new FileStream(@"d:\xmlFormat.xml", FileMode.Open))
{
using (XmlReader xmlReader = XmlReader.Create(xmlStream))
{
XmlSerializer serializer = new XmlSerializer(typeof(Response));
Response res = serializer.Deserialize(xmlReader) as Response;
}
}
代码中res是反序列化的结果,数据都正确。这就有点恼火了,怎么办呢?
比较我生成的文件和API返回的XML:
下面是我生成的XML格式
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" totalCount="" resultCode="ABC" resultMessage="测试">
<profile lastName="李" firstName="爽" dateOfBirth="XX0816" age="" gender="" address="浙江省 杭州富阳市 东州工业园区3号路1号杭州龙盛工贸有限公" email="htlslll@163.com" cellEmail="" socialMedia="" homePhone="" cellPhone="" contactabiltyMail="" contactabiltyEmail="" contactabiltyCellEmail="" contactabiltyHomePhone="" contactabiltyCellPhone="" contactabiltyText="" contactabiltySocialMedia="" vipLevel="" memberId="OC540C00016824" saAccount="" classa="小姐" homeStoreCode="OC54" />
<purchaseHistory>
<departmentPurchase totalLifeTimeSale="" totalTYAmount="" lastTxn="" totalTYUnitCnt="" firstTxn="">
<department>
<department categoryName="D08" tyAmount="" tyUnitCnt="" />
<department categoryName="D01" tyAmount="" tyUnitCnt="" />
<department categoryName="D02" tyAmount="" tyUnitCnt="" />
</department>
</departmentPurchase>
<ratePurchase totalLyAmount="" totalLyRate="" totalTyAmount="" totalTyRate="" totalLifeTimeAmount="" totalLifeTimeRate="">
<rate categoryName="Retail" lyAmount="" lyRate="" lyToDate="" tyAmount="" tyRate="" tyToDate="" lifeTimeAmount="" lifeTimeRate="" lyTyChange="" />
</ratePurchase>
<storeRatePurchase>
<storeRate />
</storeRatePurchase>
</purchaseHistory>
<vipStatus issueDate="" issueShop="OC54" ptsToUpgrade="" expiryDate="" upgradeShop="" ptsToRenew="" />
<comments comment="第一场雪" />
</Response>
仔细比对发现问题
在departmentPurchase 节点下我多了一层department节点,可我是按照接口的结果定义的对象啊
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34209")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public class departmentPurchase
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string totalTYAmount { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string totalTYUnitCnt { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string firstTxn { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string lastTxn { get; set; } [System.Xml.Serialization.XmlAttributeAttribute()]
public string totalLifeTimeSale { get; set; } [System.Xml.Serialization.XmlElementAttribute("department", typeof(List<department>), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = )]
public List<department> department { get; set; }
}
怎么搞呢?在网上不停的找答案,最终都无解。但是没有放弃,答案也往往在快要放弃的时候出现
我尝试的修改了department的定义
修改前
[System.Xml.Serialization.XmlElementAttribute("department", typeof(List<department>), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = )]
public List<department> department { get; set; }
修改后
[System.Xml.Serialization.XmlElementAttribute("department", typeof(department), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = )]
public List<department> department { get; set; }
重新运行,奇迹出现
<?xml version="1.0"?>
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" totalCount="" resultCode="ABC" resultMessage="测试">
<profile lastName="李" firstName="爽" dateOfBirth="XX0816" age="" gender="" address="浙江省 杭州富阳市 东州工业园区3号路1号杭州龙盛工贸有限公" email="htlslll@163.com" cellEmail="" socialMedia="" homePhone="" cellPhone="" contactabiltyMail="" contactabiltyEmail="" contactabiltyCellEmail="" contactabiltyHomePhone="" contactabiltyCellPhone="" contactabiltyText="" contactabiltySocialMedia="" vipLevel="" memberId="OC540C00016824" saAccount="" classa="小姐" homeStoreCode="OC54" />
<purchaseHistory>
<departmentPurchase totalLifeTimeSale="" totalTYAmount="" lastTxn="" totalTYUnitCnt="" firstTxn="">
<department categoryName="D08" tyAmount="" tyUnitCnt="" />
<department categoryName="D01" tyAmount="" tyUnitCnt="" />
<department categoryName="D02" tyAmount="" tyUnitCnt="" />
</departmentPurchase>
<ratePurchase totalLyAmount="" totalLyRate="" totalTyAmount="" totalTyRate="" totalLifeTimeAmount="" totalLifeTimeRate="">
<rate categoryName="Retail" lyAmount="" lyRate="" lyToDate="" tyAmount="" tyRate="" tyToDate="" lifeTimeAmount="" lifeTimeRate="" lyTyChange="" />
</ratePurchase>
<storeRatePurchase>
<storeRate />
</storeRatePurchase>
</purchaseHistory>
<vipStatus issueDate="" issueShop="OC54" ptsToUpgrade="" expiryDate="" upgradeShop="" ptsToRenew="" />
<comments comment="第一场雪" />
</Response>
问题解决。
原因就出在解析XML节点的
typeof(department) 与 typeof(List<department>)
[C#]记一次解析XML转对象的笔记的更多相关文章
- 记一次解析XML转对象的笔记
项目中调用第三方API,返回格式是XML字符串,需要将XML反序列化为对象,格式如下: <?xml version="1.0"?> <Response xmlns ...
- JAXB解析XML为对象
JAXB支持注解将XML转化为对象,具体看一个简单的例子: <?xml version="1.0" encoding="utf-8"?> <A ...
- 解析xml的问题未解决
工作上需要解析xml,目前的逻辑是:解析xml到对象中,然后对象再插入数据库.但这存在内存溢出的风险. 今天做的另外一件事是将循环用到工作上,暂时还没有测试,是否能保证程序的重启.有待观察 ##### ...
- Java高级特性 第14节 解析XML文档(2) - SAX 技术
一.SAX解析XML文档 SAX的全称是Simple APIs for XML,也即XML简单应用程序接口.与DOM不同,SAX提供的访问模式是一种顺序模式,这是一种快速读写XML数据的方式.当使用S ...
- Android SAX解析XML
本篇讲解一下SAX解析XML这种方式,首先来看一下它的基本介绍: SAX是一种以事件驱动的XML API,由它定义的事件流可以指定从解析器传到专门的处理程序的代码的XML结构,简单的讲,它是种解析速度 ...
- Java xml 操作(Dom4J修改xml + xPath技术 + SAX解析 + XML约束)
1 XML基础 1)XML的作用 1.1 作为软件配置文件 1.2 作为小型的"数据库" 2)XML语法(由w3c组织规定的) 标签: 标签名不能以数字开头,中间不能有空格,区分大 ...
- 利用Java反射机制完成XML到对象的解析
对于一些小批量的数据,如果采用数据库来存取的话,未免有点大题小作,使用XML文件是个不错的方法,尤其是在一些Web应用中,经常需要缓存一部分数据,如果将这些数据形成XML文件,解析后放入一个Hasht ...
- webservice04#对象与xml转换-jaxb#Stax解析xml#新建修改xml
1,Student类 package com.yangw.xml; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement / ...
- 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件
XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...
随机推荐
- 使用PerfView监测.NET程序性能(三):分组
在上一篇博客中,我们通过Perfview帮助文件中自带的代码来简单使用了Perfview,了解了基本操作.现在来看看Perfview中的分组操作(Grouping).分组功能都旨将记录到的各种函数调用 ...
- 电子书推荐--《Python灰帽子》,python黑客编程
点此在线阅读 <Python灰帽子>是由知名安全机构Immunity Inc的资深黑帽Justin Seitz主笔撰写的一本关于编程语言Python如何被广泛应用于黑客与逆向工程领域的书籍 ...
- JS学习笔记7_表单脚本
1.获取表单及表单元素引用的方式 var mForm = document.forms[formName];获取表单引用 mForm.elements[elemName]获取表单元素,如有同名的,则得 ...
- 获取用户真实ip
public static string GetRealIP() { string result = System.Web.HttpContext.Current.Request.Headers[&q ...
- 【C#】简单的发送socket字符串
1 打开VS,新建一个C#窗口程序 2 添加按钮 3 写按钮的事件代码 双击这个按钮 进入代码界面 输入如下内容,注意IP和端口 private void button1_Click(object s ...
- WPF---DataGrid设置列的百分比宽度 & 列值显示格式化
<DataGrid Height="Auto" Width="Auto"> <DataGrid.Columns> <DataGri ...
- C/C++掌握技能(一)
1.在编译器中输入代码并将其保存为.cpp文件(C语言的文件扩展名.c,但为了使用C++中的一些好用的特性,请把文件扩展名改为C++的.cpp)2.等价头文件:#include<stdio.h& ...
- 18_python_类关系
一.类与类之间的关系 1.依赖关系 class Elephant: def __init__(self, name): self.name = name def open(self, ...
- C#6.0语言规范(九) 命名空间
C#程序使用命名空间进行组织.命名空间既可以用作程序的“内部”组织系统,也可以用作“外部”组织系统 - 一种呈现暴露给其他程序的程序元素的方式. 提供了使用指令(使用指令)以便于使用命名空间. 编译单 ...
- Redis---基础数据结构
1.String(字符串) 1.1 概述 字符串 string 是 Redis 最简单的数据结构.Redis 所有的数据结构都是以唯一的 key 字符串作为名称,然后通过这个唯一 key 值来获取相应 ...