关于在C#中解析XML字符串的问题.

skming美女相册 2008-01-15 10:04:39
调用了一个外部 的servlet返回来一个字符串数组,字符串里的内容是XML格式的 byte[] byRemoteInfo

<row><loginname>chentianya</loginname><loginpwd>25d55ad283aa400af464c76d713c07ad</loginpwd><username>%E7%9C%81%E5%8F%91%E6%94%B9%E5%A7%94%E7%94%B5%E5%AD%90%E7%9B%91%E5%AF%9F%E7%94%A8%E6%88%B7</username><email>0</email><mobile>0</mobile><parentid>ff80808116e773020116eb07e91a0044</parentid><othertype>0</othertype><usersort>1</usersort></row>

我现在想把byte[] byRemoteInfo里的东西按照接点一条条读到相应的变量里.
找了很多都是从外部读XML文件的代码和例子可是我这里已经读到字符串里了呀.
请大家帮忙.
...全文
1114 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jaychou1 2009-02-24
  • 打赏
  • 举报
回复
dddddd
myh0305 2008-01-15
  • 打赏
  • 举报
回复
2楼的,LoadXml就可以了
symbol441 2008-01-15
  • 打赏
  • 举报
回复
^_^LS何出此言呢,写的不错呢
he_8134 2008-01-15
  • 打赏
  • 举报
回复
我发现自己真的很无聊~~
  • 打赏
  • 举报
回复
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
str就是你转成的字符串。
he_8134 2008-01-15
  • 打赏
  • 举报
回复
C#中byte[]与string的转换
1、
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] inputBytes =converter.GetBytes(inputString);
string inputString = converter.GetString(inputBytes);
2、
string inputString = System.Convert.ToBase64String(inputBytes);
byte[] inputBytes = System.Convert.FromBase64String(inputString);


1、
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] inputBytes =converter.GetBytes(inputString);
string inputString = converter.GetString(inputBytes);
2、
string inputString = System.Convert.ToBase64String(inputBytes);
byte[] inputBytes = System.Convert.FromBase64String(inputString);


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml = "<row><loginname>chentianya</loginname><loginpwd>25d55ad283aa400af464c76d713c07ad</loginpwd><username>%E7%9C%81%E5%8F%91%E6%94%B9%E5%A7%94%E7%94%B5%E5%AD%90%E7%9B%91%E5%AF%9F%E7%94%A8%E6%88%B7</username><email>0</email><mobile> 0 </mobile><parentid>ff80808116e773020116eb07e91a0044</parentid><othertype>0</othertype><usersort>1</usersort></row>";
RowRecord info = RowRecord.CreateFromXMLText(xml);
Console.WriteLine(info);
Console.Read();
}
}
public class RowRecord {
public string LoginName;
public string LoginPassword;
public string UserName;
public string Email;
public string Mobile;
public string ParentId;
public string OtherType;
public string UserSort;
RowRecord() { }
static Regex reg;
public static RowRecord CreateFromXMLText(string xml) {
if (reg == null) {
string pLoginName = GetTagPattern("loginname");
string pLoginPassword = GetTagPattern("loginpwd");
string pUserName = GetTagPattern("username");
string pEmail = GetTagPattern("email");
string pMobile = GetTagPattern("mobile");
string pParentId = GetTagPattern("parentid");
string pOtherType = GetTagPattern("othertype");
string pUserSort = GetTagPattern("usersort");
string pattern = string.Format("(?:{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7})+",
pLoginName,
pLoginPassword,
pUserName,
pEmail,
pMobile,
pParentId,
pOtherType,
pUserSort);
reg = new Regex(pattern,RegexOptions.Compiled);
}
Match m = reg.Match(xml);
if (m.Success) {
RowRecord info = new RowRecord();
info.LoginName = m.Groups["loginname"].Value;
info.LoginPassword = m.Groups["loginpwd"].Value;
info.UserName = m.Groups["username"].Value;
info.Email = m.Groups["email"].Value;
info.Mobile = m.Groups["mobile"].Value;
info.ParentId = m.Groups["parentid"].Value;
info.OtherType = m.Groups["othertype"].Value;
info.UserSort = m.Groups["usersort"].Value;
return info;
}
return null;
}
static string GetTagPattern(string tagName) {
return string.Format("<{0}>(?<{0}>[^<>]*)</{0}>", tagName);
//如果有空格或者换行的话
//return string.Format("<{0}>\s*(?<{0}>[^<>]*)\s*</{0}>", tagName);
}
public override string ToString()
{
return string.Format("LoginName:{0}\nLoginPassword:{1}\nUserName:{2}\nEmail:{3}\nMobile:{4}\nParentId:{5}\nOtherType:{6}\nUserSort:{7}",
LoginName,LoginPassword,UserName,Email,Mobile,ParentId,OtherType,UserSort);
}
}
}

shiling_02404 2008-01-15
  • 打赏
  • 举报
回复
居然结贴了。。。这么快
shiling_02404 2008-01-15
  • 打赏
  • 举报
回复
确实只要LoadXml就可以,不过只完成了第一步,楼主的意思是还要取得每个结点的值读到变量里:
简单点(方法中的代码如下):
string strTemp = "<row><loginname> chentianya </loginname> <loginpwd> 25d55ad283aa400af464c76d713c07ad </loginpwd> <username> %E7%9C%81%E5%8F%91%E6%94%B9%E5%A7%94%E7%94%B5%E5%AD%90%E7%9B%91%E5%AF%9F%E7%94%A8%E6%88%B7 </username> <email> 0 </email> <mobile> 0 </mobile> <parentid> ff80808116e773020116eb07e91a0044 </parentid> <othertype> 0 </othertype> <usersort> 1 </usersort> </row>";
string strLoginName=string.Empty;
string strLoginPassword = string.Empty;
string strUserName = string.Empty;
string strEmail = string.Empty;
string strMobile = string.Empty;
string strParentId = string.Empty;
string strOtherType = string.Empty;
string strUserSort = string.Empty;

XmlDocument xd = new XmlDocument();
xd.LoadXml(strTemp.Trim());
XmlNode xnRoot = xd.SelectSingleNode("row");
if (xnRoot != null)
{
strLoginName = xnRoot.ChildNodes[0].InnerText.ToString();
strLoginPassword = xnRoot.ChildNodes[1].InnerText.ToString();
strUserName=xnRoot.ChildNodes[2].InnerText.ToString();
strEmail=xnRoot.ChildNodes[3].InnerText.ToString();
strMobile=xnRoot.ChildNodes[4].InnerText.ToString();
strParentId=xnRoot.ChildNodes[5].InnerText.ToString();
strOtherType=xnRoot.ChildNodes[6].InnerText.ToString();
strUserSort = xnRoot.ChildNodes[7].InnerText.ToString();
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧