Search code examples
c#xmlxliff

How can I get the XML-part between "quotation marks" in C#?


I am writing a program in C#, to convert XML(XLF) to JSON.

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

How can I get the group id?


This is what I already have:

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

Solution

  • What you're looking for is an attribute value.

    I'd strongly suggest using LINQ to XML (XDocument etc) rather than XmlDocument - it's a much more modern API. In this case, you could use:

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

    If this is actually part of a larger document, you might use something like:

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