【问题标题】:How can I get the XML-part between "quotation marks" in C#? [duplicate]如何在 C# 中获取“引号”之间的 XML 部分? [复制]
【发布时间】:2018-07-18 08:38:31
【问题描述】:

我正在用 C# 编写一个程序,将 XML(XLF) 转换为 JSON。

<group id="THISisWHATiWANT">
    <trans-unit id="loadingDocument" translate="yes" xml:space="preserve">
                <source>Harry</source>
                <target state="final">Potter1</target>
            </trans-unit>
  </group>

我如何获得组 ID?


这是我已经拥有的:

 XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

【问题讨论】:

  • @nvoigt 我不知道这叫“属性值”所以我找不到这个问题。

标签: c# xml xliff


【解决方案1】:

您正在寻找的是一个属性值。

我强烈建议使用 LINQ to XML(XDocument 等)而不是 XmlDocument - 这是一个更现代的 API。在这种情况下,您可以使用:

XDocument doc = XDocument.Parse(xml);
string groupId = doc.Root.Attribute("id").Value;

如果这实际上是较大文档的一部分,您可能会使用以下内容:

XDocument doc = XDocument.Parse(xml);
XElement group = doc.Descendants("group").First();
string groupId = group.Attribute("id").Value;

【讨论】:

    猜你喜欢
    • 2010-12-01
    • 2020-04-01
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    相关资源
    最近更新 更多