标签: linq-to-xml

LINQ to XML新手问题:按节点名称返回节点

问候!

如果我有这样的XML:

<Root>
    <AlphaSection>
    .
    .
    .
    </AlphaSection>

    <BetaSection>
        <Choices>
            <SetA>
                <Choice id="choice1">Choice One</Choice> 
                <Choice id="choice2">Choice Two</Choice>
            </SetA>
            <SetB>
                <Choice id="choice3">Choice Three</Choice> 
                <Choice id="choice4">Choice Four</Choice>
            </SetB>
        </Choices>
    </BetaSection>

    <GammaSection>
    .
    .
    .
    </GammaSection>
</Root>
Run Code Online (Sandbox Code Playgroud)

我想获得"BetaSection"中的所有Choice项目,无论它们属于哪个"Set".我尝试过以下方法:

var choiceList = from choices in myXDoc.Root.Element("BetaSection").Elements("Choices")
                 where (choices.Name == "Choice")
                 select new
                 {
                     Name = choices.Attribute("id").Value,
                     Data = choice.Value
                 };
Run Code Online (Sandbox Code Playgroud)

但无济于事.我该怎么做?

谢谢.

c# xml linq-to-xml c#-3.0

0
推荐指数
1
解决办法
4620
查看次数

LINQ中的Count,orderby和group

给定一套

巴黎纽约伦敦纽约巴黎巴黎

我正在尝试进行LINQ查询,该查询将返回巴黎,纽约和伦敦的分组以及各自的计数.

我已经能够在每个分组中做一个返回仅包含巴黎/纽约/伦敦的组的GroupBy.

听起来比较简单,但我不能让它起作用?

编辑:这不是一个家庭作业问题.

c# linq linq-to-xml

0
推荐指数
1
解决办法
409
查看次数

在iPhone上编写xml文件(Linq = to = Xml和MonoTouch)?

我需要阅读,编辑保存一个xml文件.换句话说,我想再次在同一个文件上写,而不是创建一个新的xml文件.阅读和编辑零件有效,但不能保存.

用于保存的Linq-to-Xml代码很简单:

doc.Save(this.Path);
Run Code Online (Sandbox Code Playgroud)

适用iPhone模拟器,但在设备上抛出System.UnauthorizedAccessException.xml文件在MonoDevelop中被装饰为"内容".

任何帮助赞赏.

/ POM

iphone linq-to-xml xamarin.ios

0
推荐指数
1
解决办法
2593
查看次数

关于LINQ to XML的一个简单问题

<root xmlns:h="http://www.w3.org/TR/html4/"
      xmlns:f="http://www.w3schools.com/furniture">

<h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

</root>
Run Code Online (Sandbox Code Playgroud)

我正在尝试练习LinqToXml,但我无法弄清楚我想要什么.我怎么能查询具有h或f命名空间的表元素?这就是我尝试过的.我尝试了不同的但是没有用.

var query = from item in XDocument.Parse(xml).Elements(ns + "table")
            select item;
Run Code Online (Sandbox Code Playgroud)

.net c# namespaces linq-to-xml

0
推荐指数
1
解决办法
335
查看次数

为什么XDocument.Descendants().Count()> 0表示空文档?

这是我的方法:

var document = XDocument.Parse(source);
if (document.Descendants().Count() > 0)
{
    // Some code that shouldn't execute
}
else
{
    // Code that should execute
}
Run Code Online (Sandbox Code Playgroud)

这个代码在'document'变量中时会中断:

<ipb></ipb>
Run Code Online (Sandbox Code Playgroud)

由于这不具有后代,为什么它进入IF条件?是不应该尝试加载任何东西,但它确实并且在它找不到任何东西时会中断.

使用断点我可以确认文档变量具有我在上面发布的内容,并且它确实输入了if.

c# linq-to-xml

0
推荐指数
1
解决办法
1889
查看次数

LINQ to XML - 如何阅读此XML?

我有一个XML文件,如下所示:

...
<body>

<unit id="1" name ="xxx">
<sourceFile>SomeFile.xml</sourceFile>
<targetFile/>
</unit>

<unit id="2" name ="xxx">
<sourceFile>SomeFile.xml</sourceFile>
<targetFile/>
</unit>

</body>
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何通过C#使用LINQ to XML来读取sourceFile节点的值,并更新targetFile的值,因为我不熟悉LINQ to XML?

谢谢.

c# linq-to-xml

0
推荐指数
1
解决办法
428
查看次数

使用XDeclaration将文档类型添加到XML

我正在尝试将XDeclaration添加到XML文档中,如下所示:

XDocument doc = new XDocument();
XDeclaration dc = new XDeclaration("1.0", "utf-8", "no");
XNamespace ns = "http://www.foo.com/bar";

doc.Add(dc);
Run Code Online (Sandbox Code Playgroud)

但是,出现以下错误:

不能将非空格字符添加到内容中。

如果我删除XDeclaration行,则代码可以正常工作,我在哪里出错?

c# xml linq-to-xml

0
推荐指数
1
解决办法
886
查看次数

如何复制xml文件并重命名并使用c#保存在同一个根目录中?

我是否需要递归地使用linq to xml并将每个节点和元素从一个xml复制到另一个xml或者是否有更好的方法通过重命名直接复制并将其保存在同一个根目录中?任何人都可以举个例子

c# xml linq-to-xml c#-3.0 c#-4.0

0
推荐指数
1
解决办法
3879
查看次数

有没有更优雅的方法来使用linq来设置属性值?

我有以下代码模糊,它将选择我的XML中具有RatingNumericValue的所有数据元素,然后将该数据元素的值设置为RatingNumericValue的内容.我想知道这是否适合使用谓词.有没有更好的方法使用linq来做到这一点?

    Dim dataEls = From item In copy...<Table>...<Row>...<Cell>...<Data> Select item Where item.@RatingNumericValue IsNot Nothing
    For Each de In dataEls
        de.Value = de.@RatingNumericValue
    Next
Run Code Online (Sandbox Code Playgroud)

.net linq vb.net linq-to-xml

0
推荐指数
1
解决办法
75
查看次数

XDocument用小写替换所有节点值

我需要在XDocument小写中创建所有值.这样做的最佳方法是什么?

为了澄清,我想更改所有文本值,而不指定任何节点的名称.

linq-to-xml

0
推荐指数
1
解决办法
1484
查看次数

标签 统计

linq-to-xml ×10

c# ×7

xml ×3

.net ×2

c#-3.0 ×2

linq ×2

c#-4.0 ×1

iphone ×1

namespaces ×1

vb.net ×1

xamarin.ios ×1