.net 将一个xml格式的文件转换为string类型数组 15
2个回答
展开全部
思路如下:
首先,定义一个数组;
然后,遍历xml里面的数据一个一个保存到list数组中;
最后返回数组。
简单代码如下:
//formXml XML内容
public List<string> getFormData(string formXml)
{
List<string> list = new List<string>();
if (string.IsNullOrEmpty(formXml)) return list;
XDocument doc = XDocument.Parse(formXml);
XElement root = doc.Root;
foreach (var node in root.Descendants())
{
string currentName = "";
string currentPath = "";
if (node.HasElements)
continue;
XElement currentNode = node;
while (currentNode != node.Document.Root)
{
currentPath = currentNode.Name;
currentName = currentNode.Parent;
}
list.Add("XML节点名称:" + currentPath + ",内容:" + currentName);
}
return list;
}
首先,定义一个数组;
然后,遍历xml里面的数据一个一个保存到list数组中;
最后返回数组。
简单代码如下:
//formXml XML内容
public List<string> getFormData(string formXml)
{
List<string> list = new List<string>();
if (string.IsNullOrEmpty(formXml)) return list;
XDocument doc = XDocument.Parse(formXml);
XElement root = doc.Root;
foreach (var node in root.Descendants())
{
string currentName = "";
string currentPath = "";
if (node.HasElements)
continue;
XElement currentNode = node;
while (currentNode != node.Document.Root)
{
currentPath = currentNode.Name;
currentName = currentNode.Parent;
}
list.Add("XML节点名称:" + currentPath + ",内容:" + currentName);
}
return list;
}
展开全部
//formXml XML内容
public List<string> getFormData(string formXml)
{
List<string> list = new List<string>();
if (string.IsNullOrEmpty(formXml)) return list;
XDocument doc = XDocument.Parse(formXml);
XElement root = doc.Root;
foreach (var node in root.Descendants())
{
string currentName = "";
string currentPath = "";
if (node.HasElements)
continue;
XElement currentNode = node;
while (currentNode != node.Document.Root)
{
currentPath = currentNode.Name;
currentName = currentNode.Parent;
}
list.Add("XML节点名称:" + currentPath + ",内容:" + currentName);
}
return list;
}
public List<string> getFormData(string formXml)
{
List<string> list = new List<string>();
if (string.IsNullOrEmpty(formXml)) return list;
XDocument doc = XDocument.Parse(formXml);
XElement root = doc.Root;
foreach (var node in root.Descendants())
{
string currentName = "";
string currentPath = "";
if (node.HasElements)
continue;
XElement currentNode = node;
while (currentNode != node.Document.Root)
{
currentPath = currentNode.Name;
currentName = currentNode.Parent;
}
list.Add("XML节点名称:" + currentPath + ",内容:" + currentName);
}
return list;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询