XML在C#中解析并转换为对象 [英] XML Parsing in C# and convert into object
本文介绍了XML在C#中解析并转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我是.Net开发中的xml新手,
你愿意吗?请告诉我能够将xml字符串解析为键值对的方法。
我的xml字符串如下所示
< position >
< id > 56565 < / id >
< title > 分析师< / title >
< 摘要 > 应用程序编程,数据库编程,应用程序支持和维护< / summary >
< 开始日期 >
< ; 年 > 2013 < / year >
< ; 月 > 7 < / month >
< ; / start-date >
< is-current > true < / is-current >
< 公司 >
< id > 434 < / id >
< name > xyz limited < / name >
< / company >
< ; / position >
提前感谢
解决方案
使用 XmlDocument [ ^ ]并调用 LoadXml [ ^ ]方法。
这将帮助您将XML转换为对象
XML序列化和反序列化:第1部分 [ ^ ]
你可以试试这个
我考虑了以下源XML
< root >
< position >
< id > 1 < / id >
< title > ABC < title >
< company > PQR < span class =code-keyword>< company >
< / company > < / company > < / title > < / title > < / position > ;
< / root >
和目标XML为
< root >
< / root >
Code Goes Here
XmlDocument xDoc = new XmlDocument();
XmlDocument xDoc2 = new XmlDocument();
xDoc.Load(HttpContext.Current.Server.MapPath( menu.xml); // source xml
xDoc2.Load(HttpContext.Current.Server .MapPath( menu1.xml); // target xml
XmlNode Page = null ;
string [] str = { id, title, company} // 您的逻辑属性
XmlNode Data = xDoc2.CreateNode(XmlNodeType.Element, positio n, null );
for ( int i = 0 ; i < 3 ; i ++)
{
Page = xDoc.SelectSingleNode( position / + str [i]);
XmlAttribute item = xDoc2.CreateAttribute(str [i]);
item.Value = Page.InnerText;
Data.Attributes.Append(item);
xDoc2.DocumentElement.AppendChild(Data);
}
xDoc2.Save(HttpContext.Current.Server.MapPath( menu1.xml);
你的OutPut将是
< root >
< position id = 1 title = ABC 公司 = PQR / >
< / root > 跨度>
Hi All,
I am new to xml in .Net Development,
will you please tell me the method through which i can able to parse xml string into key value pair.
my xml string is like given below
<position>
<id>56565</id>
<title>Analyst</title>
<summary>Application Programming,Database Programming,Application Support and Maintenance</summary>
<start-date>
<year>2013</year>
<month>7</month>
</start-date>
<is-current>true</is-current>
<company>
<id>434</id>
<name>xyz limited</name>
</company>
</position>
thanks in advance
解决方案
Use an XmlDocument[^] and call the LoadXml[^] method.
This will help you convert XML to Object
XML Serialization and Deserialization: Part-1[^]
You Can Try this
I have Considered the following Source XML
<root> <position> <id> 1 </id> <title> ABC <title> <company> PQR <company> </company></company></title></title></position> </root>
And Target XML as
<root> </root>
Code Goes Here
XmlDocument xDoc = new XmlDocument(); XmlDocument xDoc2 = new XmlDocument(); xDoc.Load(HttpContext.Current.Server.MapPath("menu.xml"); // source xml xDoc2.Load(HttpContext.Current.Server.MapPath("menu1.xml"); // target xml XmlNode Page = null; string[] str = {"id","title","company"} //Your attributes with your logic XmlNode Data = xDoc2.CreateNode(XmlNodeType.Element,"position",null); for(int i = 0 ; i < 3 ; i++) { Page = xDoc.SelectSingleNode("position/" + str[i]); XmlAttribute item = xDoc2.CreateAttribute(str[i]); item.Value = Page.InnerText; Data.Attributes.Append(item); xDoc2.DocumentElement.AppendChild(Data); } xDoc2.Save(HttpContext.Current.Server.MapPath("menu1.xml");
Your OutPut will be
<root> <position id=1 title=ABC company=PQR /> </root>
这篇关于XML在C#中解析并转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文