C# XDocument的XElement解析,远低于,重复2次
昨天我问了同样的问题。 在一个好人的帮助下,我漂亮地解决了这个问题 现在,我发现我丢失了一件物品。 我想这样做C# XDocument的XElement解析,远低于,重复2次,c#,xml,linq,C#,Xml,Linq,昨天我问了同样的问题。 在一个好人的帮助下,我漂亮地解决了这个问题 现在,我发现我丢失了一件物品。 我想这样做 1 Boo1 // "1" is from <sdl:seg id="1"> 2 Boo2 3 Boo3 ... .. . 1 Boo1//“1”来自 2 Boo2 3 Boo3 ... .. . 从这里开始 <xliff xmlns:sdl="http://sdl.com/FileTypes/SdlXliff/1.0"> <sdl:
1 Boo1 // "1" is from <sdl:seg id="1">
2 Boo2
3 Boo3
...
..
.
1 Boo1//“1”来自
2 Boo2
3 Boo3
...
..
.
从这里开始
<xliff xmlns:sdl="http://sdl.com/FileTypes/SdlXliff/1.0">
<sdl:seg-defs>
<sdl:seg id="1">
<sdl:value key="Hash">Foo1</sdl:value>
<sdl:value key="Created">Boo1</sdl:value>
Foo1
Boo1
我正试着这样做
XDocument myDoc = XDocument.Load(myPath);
Dictionary<string, XElement> myDic = myDoc.Descendants()
.Where(x => x.Name.LocalName == "seg")
.Select((myValue, myKey) => new { myKey, myValue })
.ToDictionary(x => x.Attribute("id").Value, x => x); // Error at .Attribute(
List<string> myList = myDic
.Where(x => ((x.Value).Name.LocalName == "value")
&& ((string)(x.Value).Attribute("key") == "Created"))
.Select(x.Key + " " + (string)x.Value);
MessageBox.Show(string.Join("\n", myList));
XDocument myDoc=XDocument.Load(myPath);
字典myDic=myDoc.subjects()
.Where(x=>x.Name.LocalName==“seg”)
.Select((myValue,myKey)=>new{myKey,myValue})
.ToDictionary(x=>x.Attribute(“id”).Value,x=>x);//属性(
列表myList=myDic
.Where(x=>((x.Value).Name.LocalName==“Value”)
&&((字符串)(x.Value).Attribute(“key”)=“Created”))
.选择(x.Key++(字符串)x.Value);
MessageBox.Show(string.Join(“\n”,myList));
谢谢
var q = from elem in myDoc.Descendants()
where elem.Name.LocalName == "seg"
from sub in elem.Descendants()
where sub.Name.LocalName == "value" && sub.Attribute("key").Value == "Created"
select new
{
Id = elem.Attribute("id").Value,
Created = sub.Value
};
通过正确的命名空间处理:
XNamespace sdl = "http://sdl.com/FileTypes/SdlXliff/1.0";
var q = from elem in myDoc.Descendants()
where elem.Name == sdl + "seg"
from sub in elem.Descendants()
where sub.Name== sdl + "value" && sub.Attribute("key").Value == "Created"
select new
{
Id = elem.Attribute("id").Value,
Created = sub.Value
};
并将其格式化:
var msg = String.Join("\n", q.Select(item => $"{item.Id} {item.Created}"));
试试这个
Dictionary<string, List<XElement>> myDic = myDoc.Descendants()
.Where(x => x.Name.LocalName == "seg")
.GroupBy(myKey => (string)myKey.Attribute("id"))
.ToDictionary(x => x.Key, y => y.Descendants().Where(z => z.Name.LocalName == "value").ToList());
Dictionary<string, Dictionary<string,string>> myDic2 = myDoc.Descendants()
.Where(x => x.Name.LocalName == "seg")
.GroupBy(myKey => (string)myKey.Attribute("id"))
.ToDictionary(x => x.Key, y => y.Descendants().Where(z => z.Name.LocalName == "value")
.GroupBy(z => (string)z.Attribute("key"), a => (string)a)
.ToDictionary(z => z.Key, a => a.FirstOrDefault()));
字典myDic=myDoc.subjects()
.Where(x=>x.Name.LocalName==“seg”)
.GroupBy(myKey=>(字符串)myKey.Attribute(“id”))
.ToDictionary(x=>x.Key,y=>y.subjects()。其中(z=>z.Name.LocalName==“value”).ToList();
字典myDic2=myDoc.subjects()
.Where(x=>x.Name.LocalName==“seg”)
.GroupBy(myKey=>(字符串)myKey.Attribute(“id”))
.ToDictionary(x=>x.Key,y=>y.subjects()。其中(z=>z.Name.LocalName==“value”)
.GroupBy(z=>(字符串)z.Attribute(“键”),a=>(字符串)a)
.ToDictionary(z=>z.Key,a=>a.FirstOrDefault());
XNamespace…我真的很感激。再次。我很高兴听到这个消息。问候。发现拼写错误。还提供了第二个解决方案。哇哦。太棒了!!感谢你的努力、精力和(最重要的)时间!!工作正常!!哪种解决方案?1或2。你说的“1”和“2”是什么意思?你的全部评论。我使用了“myDic”和“myDic2”连续。您知道元素()和子元素()之间的区别吗?一旦获得第一个for循环和第二个for循环中的元素,可能需要使用子元素。元素仅适用于元素的子元素。子元素必须使用子元素。