标签: linq-to-xml

为什么XDocument.Descendants()在PowerShell ISE中返回IEnumerator?

我正在编写一个PowerShell脚本来操作一些Windows Installer XML(WiX).我正在使用.NET 3.5中的新XML API来实现这一点,因为我发现它比DOM更容易使用API​​.以下脚本片段断然拒绝工作:

[System.Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") | Out-Null

# Basic idea: Iterate through en.wxl's l10ns, get string for each l10n, search all wxs files for that value.

pushd "C:\temp\installer_l10n"
$wxlFileName = "${pwd}\en.wxl"
$wxl = [System.Xml.Linq.XDocument]::Load($wxlFileName)
$strings = $wxl.Descendants("String")
$strings
$strings | foreach {
    $_
}
popd
Run Code Online (Sandbox Code Playgroud)

该脚本在单独的行上输出每个<String>标记.一旦这个bug得到解决,我就会让它做一些更有趣的事情;-)

XML文档是标准的WiX本地化文件:

<?xml version="1.0" encoding="utf-8" ?>
<WixLocalization Culture="en-US" xmlns="http://schemas.microsoft.com/wix/2006/localization">
   <String Id="0">Advertising published resource</String>
   <String Id="1">Allocating registry space</String>
   ...
</WixLocalization>
Run Code Online (Sandbox Code Playgroud)

$ strings不是$ null(我已经明确地测试了它),如果我写了host-wxl,我可以看到文件已被加载.将$字符串管理到Get-Member会返回一个错误,指出"没有为get-member指定任何对象",write-host $字符串不执行任何操作.我也尝试过$ wxl.Descendants("WixLocalization"),结果相同.像$ wxl.Root和$ wxl.Nodes这样的东西按预期工作.使用PowerShell ISE进行调试,我看到$ strings已设置为IEnumerator,而不是预期的IEnumerable <XElement>.使用单个MoveNext测试IEnumerator,然后使用Current测试"Current =",大概是$ null.

奇怪的是,相同的技术在以前的脚本中起作用.完全相同的代码,但具有不同的变量名和字符串文字.刚刚尝试调试该脚本(以验证行为),它似乎现在也显示相同的行为.

xml powershell linq-to-xml powershell-ise powershell-2.0

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

使用LINQ将XML标记的集合连接到字符串

我无法使用我无法控制的Web服务,并且正在尝试将该服务返回的XML解析为标准对象.

XML结构的一部分看起来像这样

<NO>
   <L>Some text here </L>
   <L>Some additional text here </L>
   <L>Still more text here </L>
</NO>
Run Code Online (Sandbox Code Playgroud)

最后,我想最终得到一个String属性,看起来像"有些文本在这里一些额外的文字这里还有更多的文字在这里"

我对初始通行证的内容如下.我想我已经走上了正轨,但还没到那里:

XElement source = \\Output from the Webservice
List<IndexEntry> result;

result = (from indexentry in source.Elements(entryLevel)
    select new IndexEntry()
    {
        EtiologyCode = indexentry.Element("IE") == null ? null : indexentry.Element("IE").Value,
        //some code to set other properties in this object
        Note = (from l in indexentry.Elements("NO").Descendants
                select l.value)  //This is where I stop
                               // and don't know where to go
    }
Run Code Online (Sandbox Code Playgroud)

我知道我可以在该查询的末尾添加一个ToList()运算符来返回该集合.是否有一个opertaor或技术允许我将该集合的连接内联到一个字符串? …

c# linq linq-to-xml

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

插入XElement以使用LinqToXml包围另一个XElement

假设我有以下xml,

<Where><BeginsWith>...</BeginsWith></Where>
Run Code Online (Sandbox Code Playgroud)

现在我想"插入"一个<And>围绕BeginsWith子句的子句,所以它看起来像这样,

<Where><And><BeginsWith>...</BeginsWith></And></Where>
Run Code Online (Sandbox Code Playgroud)

如何使用LinqToXml实现这一目标?

我基本上做的Add方法 where.Add(new XElement("And"))只会在BeginsWith之后添加"And",就像这样,

<Where><BeginsWith>...</BeginsWith><And /></Where>
Run Code Online (Sandbox Code Playgroud)

c# linq-to-xml

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

如何使用LINQ to XML将XML元素移动到上一个元素之上?

我有以下XML结构:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root xmlns:xsi="My Program">
    <NotRoot Text="Hello">
        <SomeOption Text="Option 1" Centered="False">
            <SomeOption Text="Option 1.1" Centered="False">
                <SomeOption Text="Option 1.1.1" Centered="false">
                    <SomeOption Text="A" Centered="false">
                        <SpecialName Text="Blah blah" Centered="false">
                            <Number>1</Number>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="B" Centered="false">
                        <SpecialName Text="Hi" Centered="true">
                            <SomeStrangeName>42</SomeStrangeName>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="C" Centered="false">
                        <SpecialName Text="Some text here" Centered="false">
                            <Stuff>
                                <Thing1>10</Thing1>
                                <Thing2>20</Thing2>
                                <Thing3>30</Thing3>
                            </Stuff>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="D" Centered="false">
                        <SpecialName Text="Bye" Centered="false">
                            <Things>
                                <Random1>9846516981</Random1>
                                <Random2>8784749874</Random2>
                            </Things>
                        </SpecialName>
                    </SomeOption>
                </SomeOption>
            </SomeOption>
        </SomeOption>
    </NotRoot>
</Root>
Run Code Online (Sandbox Code Playgroud)

我想将元素"C"向上移动一个位置,以便输出如下所示:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root xmlns:xsi="My …
Run Code Online (Sandbox Code Playgroud)

.net c# xml linq-to-xml

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

C#获取XML数据的最简单方法

我已经在LINQ to XML上做了很多阅读,但遗憾的是这个主题(对我来说相当新)根本不会点击.话虽如此,请随时纠正有关正确的XML词汇表的任何错误说法.我的目标是获取XML数据(如下所示),并逐节点地读取它.在这种情况下,我希望能够打开Delimiters节点,以获得" one "," two "和" three "元素的值.接下来,我想从Sources/SourceType节点中获取" one "," two "和" three "元素的值.

<?xml version="1.0"?>
<Values>

  <Delimiters>
    <one>delim 1</one>
    <two>delim 2</two>
    <three>delim 3</three>
  </Delimiters>

  <Sources>

    <SourceType>
      <one>type 1</one>
      <two>type 2</two>
      <three>type 3</three>
    </SourceType>

  </Sources>

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

我已经阅读XMLTextReader过,XMLReader但我想听听你们所有人对我这种情况的最佳做法.

谢谢你的阅读,

埃文

c# xml linq-to-xml readxml

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

如何查询此XML文件?

我很难尝试创建查询.这是文件:

<Root>
<Summary>
    <Objective ID="1">
        <Exam Result="70" />
        <Exam Result="90" />
    </Objective>
    <Objective ID="2">
        <Exam Result="100" />
        <Exam Result="90" />
    </Objective>
</Summary>
</Root>
Run Code Online (Sandbox Code Playgroud)

我需要获取List <List <double >>中的值.第一个列表是否为目标,最后一个是存储每个结果.

有任何疑问,请告诉我

c# xml linq linq-to-xml

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

如何以XML保存WPF用户设置?

我是WPF新手。我正在构建一个简单的WPF应用,该应用要求用户使用C#语言登录。在登录页面中,用户允许在“记住我”和“自动登录我”复选框上打勾,但是如果应用程序关闭,则该设置将重置为默认设置。因此,我要的是每次用户勾选复选框并关闭应用程序后,必须保存设置。

正如我在网上找到的那样,有一些方法可以保存用户设置。有人可以向我提供有关将用户设置保存到XML中的分步教程或链接吗?

PS我有一些Linq经验。我最好使用Linq to XML方法。

c# xml wpf linq-to-xml

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

如何在C#中声明具有特殊字符的字符串常量

如何在c#程序中将以下XML定义为字符串常量。

<?xml version="1.0" encoding="utf-8"?>
<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
  <head>
  <meta base="rtmp://dos.com/vevood" />
  </head>
  <body>

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

现在,我将以上内容另存为测试文件(file.txt),然后使用以下内容将其加载到xdocument中:

XDocument.Load(@"G:\file.txt");
Run Code Online (Sandbox Code Playgroud)

我想将xml保存为字符串,然后执行类似的操作:XDocument.Load(string);

问题是xml中有特殊字符。

实现目标的最简单方法是什么?

c# linq-to-xml

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

XmlDocument.Load(String filename)和XmlDocument.Load(FileStream fs)之间的区别

我已经尝试了一些关于将新子项添加到XML文件的代码.我注意到使用XmlDocument.Load(String filename)和XmlDocument.Load(FileStream fs)时结果不同.下面显示了原始XML文件数据.

<?xml version="1.0" encoding="utf-8"?>
<grandparent>
    <parent>
        <child>
            <grandchild>some text here</grandchild>
        </child>
        <child>
            <grandchild>another text here</grandchild>
        </child>
    </parent>
</grandparent>
Run Code Online (Sandbox Code Playgroud)

下面显示了使用XmlDocument.Load(String filename)附加子元素的C#代码

XmlDocument doc = new XmlDocument();
doc.Load(filename);

XmlNode child= doc.CreateNode(XmlNodeType.Element, "child", null);
XmlNode grandchild = doc.CreateNode(XmlNodeType.Element, "grandchild", null);
grandchild.InnerText = "different text here";
child.AppendChild(grandchild);
doc.SelectSingleNode("//grandparent/parent").AppendChild(child);
doc.Save(filename);
Run Code Online (Sandbox Code Playgroud)

结果XML文件正常工作,如下所示.

<?xml version="1.0" encoding="utf-8"?>
<grandparent>
    <parent>
        <child>
            <grandchild>some text here</grandchild>
        </child>
        <child>
            <grandchild>another text here</grandchild>
        </child>
        <child>
            <grandchild>different text here</grandchild>
        </child>
    </parent>
</grandparent>
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用XmlDocument.Load(FileStream fs),如下所示

FileStream fs = new FileStream(filename, FileMode.Open)
XmlDocument doc …
Run Code Online (Sandbox Code Playgroud)

c# xml linq-to-xml

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

如何找不到元素,如何解析xml文件并返回默认值

我在C#中编写了一个简单的方法来解析给定的xml文件并返回特定节点的值.它工作正常,但如果找不到节点而不是抛出异常,我也想返回默认值.我怎样才能做到这一点?这种方法可以写得更好吗?感谢您提供的任何帮助.约翰

public static string ReadConfigurationFile(string configurationFileName, string root, string section, string name)
{
    try
    {
        String currentDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        configFilePath = Directory.GetParent(currentDirectory) + configurationFolder + configurationFileName;

        XDocument configXML = XDocument.Load(configFilePath);
        var result = from setting in
                         configXML.Descendants(root)
                            .Descendants(section)
                     select setting.Element(name).Attribute("value").Value;
        return result.First();
    }
    catch (Exception ex)
    {
        return string.Empty;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我解析的XML文件的示例:

<?xml version='1.0' encoding='utf-8'?>
<automationSettings>
    <vmDomain>
        <domainName value = "Domain"/>
        <domainUsername value = "username"/>
        <domainPassword value = "password"/>
    </vmDomain>
</automationSettings>
Run Code Online (Sandbox Code Playgroud)

c# xml linq-to-xml

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

标签 统计

linq-to-xml ×10

c# ×9

xml ×7

linq ×2

.net ×1

powershell ×1

powershell-2.0 ×1

powershell-ise ×1

readxml ×1

wpf ×1