Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向XElement添加子节点_C#_Wpf_Xelement - Fatal编程技术网

C# 向XElement添加子节点

C# 向XElement添加子节点,c#,wpf,xelement,C#,Wpf,Xelement,我试图将对象附加到XML文件中。我现在的问题是,它附加了第一级本身的所有内容。我试图将列表作为父元素,将列表项作为子元素 我尝试过的:我遇到过一些帖子,他们使用循环,但我无法将其与我的上下文和代码联系起来 代码: XDocument XDocument=XDocument.Load(@“C:\Users\hci\Desktop\Nazish\tangramsool\tangramsool\patterndata.xml”); XElement root=xDocument.Element(“模式

我试图将对象附加到XML文件中。我现在的问题是,它附加了第一级本身的所有内容。我试图将列表作为父元素,将列表项作为子元素

我尝试过的:我遇到过一些帖子,他们使用循环,但我无法将其与我的上下文和代码联系起来

代码:

XDocument XDocument=XDocument.Load(@“C:\Users\hci\Desktop\Nazish\tangramsool\tangramsool\patterndata.xml”);
XElement root=xDocument.Element(“模式”);
foreach(PatternDictionary.Values中的模式currentPattern)
{
字符串filePath=currentPattern.Name.ToString();
IEnumerable rows=root.substands(“Pattern”);//按文档顺序返回此文档或元素的子元素集合。
XElement firstRow=rows.First();//返回序列的第一个元素。
if(currentPattern.PatternDistancesList.Count()==9)
{
firstRow.AddBeforeSelf(//将指定的内容直接添加到此节点之前。
新XElement(“模式”),
新的XElement(“名称”,filePath.Substring(64)),
新XElement(“PatternDistancesList”),
新建XElement(“PatternDistance”,currentPattern.PatternDistancesList[0].ToString()),
新建XElement(“PatternDistance”,currentPattern.PatternDistancesList[1].ToString()),
}
}
当前XML文件:

<Pattern/> 
<Name>match.jpg</Name> 
<PatternDistancesList/>       
<PatternDistance>278</PatternDistance>
<PatternDistance>380</PatternDistance> 

match.jpg
278
380
我想要的最终结果是:

<Pattern> 
<Name>match.jpg</Name> 
<PatternDistancesList>       
    <PatternDistance>278</PatternDistance>
    <PatternDistance>380</PatternDistance>
</PatternDistancesList> 
<Pattern/>

match.jpg
278
380

任何提示都将不胜感激。我是WPF和C的新手,因此仍在努力学习。

这应该可以做到:

firstRow.AddBeforeSelf(
    new XElement("Pattern",
        new XElement("Name", filePath.Substring(64)),
        new XElement("PatternDistancesList",
            new XElement("PatternDistance", currentPattern.PatternDistancesList[0].ToString()),
            new XElement("PatternDistance", currentPattern.PatternDistancesList[1].ToString()))));

这应该可以做到:

firstRow.AddBeforeSelf(
    new XElement("Pattern",
        new XElement("Name", filePath.Substring(64)),
        new XElement("PatternDistancesList",
            new XElement("PatternDistance", currentPattern.PatternDistancesList[0].ToString()),
            new XElement("PatternDistance", currentPattern.PatternDistancesList[1].ToString()))));

这应该可以做到:

firstRow.AddBeforeSelf(
    new XElement("Pattern",
        new XElement("Name", filePath.Substring(64)),
        new XElement("PatternDistancesList",
            new XElement("PatternDistance", currentPattern.PatternDistancesList[0].ToString()),
            new XElement("PatternDistance", currentPattern.PatternDistancesList[1].ToString()))));

这应该可以做到:

firstRow.AddBeforeSelf(
    new XElement("Pattern",
        new XElement("Name", filePath.Substring(64)),
        new XElement("PatternDistancesList",
            new XElement("PatternDistance", currentPattern.PatternDistancesList[0].ToString()),
            new XElement("PatternDistance", currentPattern.PatternDistancesList[1].ToString()))));

非常感谢,这就像一个咒语!我应该尝试将它们合并到XElement>中。非常感谢,这就像一个咒语!我应该尝试将它们合并到XElement>中。非常感谢,这就像一个咒语!我应该尝试将它们合并到XElement>中。非常感谢,这就像一个咒语!我应该已尝试在XElement>中合并它们。