C# .NET将标记转换为地图
我需要将包含标记的文本转换为一个简单的映射(字典)。文本如下所示:C# .NET将标记转换为地图,c#,.net,xml,dictionary,C#,.net,Xml,Dictionary,我需要将包含标记的文本转换为一个简单的映射(字典)。文本如下所示: <tag1>text1</tag1><tag2>text2></tag2> Dictionary<string,string> dicTags = new Dictionary<string,string>(); dicTags["tag1"] = "text1"; dicTags["tag2"] = "text2"; text1text2>
<tag1>text1</tag1><tag2>text2></tag2>
Dictionary<string,string> dicTags = new Dictionary<string,string>();
dicTags["tag1"] = "text1";
dicTags["tag2"] = "text2";
text1text2>
我需要的是这样的东西:
<tag1>text1</tag1><tag2>text2></tag2>
Dictionary<string,string> dicTags = new Dictionary<string,string>();
dicTags["tag1"] = "text1";
dicTags["tag2"] = "text2";
Dictionary dicTags=newdictionary();
dicTags[“tag1”]=“text1”;
dicTags[“tag2”]=“text2”;
如果我们事先不知道标记名,他们的方法简单吗?假设发布的XML片段被包装在单个根元素中,以便生成格式良好的XML,您可以执行以下操作来生成所需的字典:
var raw = @"<root><tag1>text1</tag1><tag2>text2</tag2> </root>";
var doc = XDocument.Parse(raw);
var dicTags = doc.Root.Elements().ToDictionary(e => e.Name.LocalName, e => (string)e);
foreach(var kv in dicTags)
{
Console.WriteLine("Key: {0}, Value: {1}", kv.Key, kv.Value);
}
假设发布的XML片段被包装在单个根元素中,以便生成格式良好的XML,您可以按照以下操作生成所需的字典:
var raw = @"<root><tag1>text1</tag1><tag2>text2</tag2> </root>";
var doc = XDocument.Parse(raw);
var dicTags = doc.Root.Elements().ToDictionary(e => e.Name.LocalName, e => (string)e);
foreach(var kv in dicTags)
{
Console.WriteLine("Key: {0}, Value: {1}", kv.Key, kv.Value);
}
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication96
{
class Program
{
static void Main(string[] args)
{
string xml = "<root><tag1>text1</tag1><tag2>text2></tag2></root>";
XElement root = XElement.Parse(xml);
Dictionary<string, string> dict1 = new Dictionary<string, string>();
//if each tag is unique
dict1 = root.Elements().GroupBy(x => x.Name.LocalName, y => y).ToDictionary(x => x.Key, y => y.FirstOrDefault().Value);
//if tag names are duplicated then use this
Dictionary<string, List<string>> dict2 = new Dictionary<string, List<string>>();
dict2 = root.Elements().GroupBy(x => x.Name.LocalName, y => y).ToDictionary(x => x.Key, y => y.Select(z => z.Value).ToList());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序96
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=“text1text2>”;
XElement root=XElement.Parse(xml);
Dictionary dict1=新字典();
//如果每个标记都是唯一的
dict1=root.Elements().GroupBy(x=>x.Name.LocalName,y=>y).ToDictionary(x=>x.Key,y=>y.FirstOrDefault().Value);
//如果标记名重复,则使用此
Dictionary dict2=新字典();
dict2=root.Elements().GroupBy(x=>x.Name.LocalName,y=>y).ToDictionary(x=>x.Key,y=>y.Select(z=>z.Value).ToList());
}
}
}
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication96
{
class Program
{
static void Main(string[] args)
{
string xml = "<root><tag1>text1</tag1><tag2>text2></tag2></root>";
XElement root = XElement.Parse(xml);
Dictionary<string, string> dict1 = new Dictionary<string, string>();
//if each tag is unique
dict1 = root.Elements().GroupBy(x => x.Name.LocalName, y => y).ToDictionary(x => x.Key, y => y.FirstOrDefault().Value);
//if tag names are duplicated then use this
Dictionary<string, List<string>> dict2 = new Dictionary<string, List<string>>();
dict2 = root.Elements().GroupBy(x => x.Name.LocalName, y => y).ToDictionary(x => x.Key, y => y.Select(z => z.Value).ToList());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序96
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=“text1text2>”;
XElement root=XElement.Parse(xml);
Dictionary dict1=新字典();
//如果每个标记都是唯一的
dict1=root.Elements().GroupBy(x=>x.Name.LocalName,y=>y).ToDictionary(x=>x.Key,y=>y.FirstOrDefault().Value);
//如果标记名重复,则使用此
Dictionary dict2=新字典();
dict2=root.Elements().GroupBy(x=>x.Name.LocalName,y=>y).ToDictionary(x=>x.Key,y=>y.Select(z=>z.Value).ToList());
}
}
}
在此链接中,我们提前知道标签名称。在我的情况下,它可以改变。在运行代码之前我无法知道标签名。对不起,这是误判。不幸的是,我无法删除这张接近票数的选票,但希望没有人关注它。在这个链接中,我们提前知道标签名称。在我的情况下,它可以改变。在运行代码之前我无法知道标签名。对不起,这是误判。不幸的是,我不能取消投票,但希望没有人遵循它。谢谢你正是我所需要的!我刚刚发布了一个版本,我还得到了一个类似于var raw=@“text1text3text2”的东西;而且它没有进入标签。修改它很简单吗?谢谢你,这正是我需要的!我刚刚发布了一个版本,我还得到了一个类似于var raw=@“text1text3text2”的东西;而且它没有进入标签。修改它这么简单吗?