Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0"?>
- <xliff version="1.2">
- <file source-language="en-US" datatype="plaintext" category="framework">
- <body>
- <transaction approved="no" id="1">
- <source xml:lang="en-US">Product Family</source>
- <target state="translated" xml:lang="en-US">Product Family</target>
- </transaction>
- </body>
- </file>
- </xliff>
- private void btnOpen_Click(object sender, EventArgs e)
- {
- // Show the dialog, using defaults, and get result
- OpenFileDialog ofdResult = new OpenFileDialog();
- if (ofdResult.ShowDialog() == DialogResult.OK)
- {
- try
- {
- if (ofdResult.OpenFile() != null)
- {
- XDocument xmlFile = XDocument.Load(ofdResult.FileName);
- // print elements recursively
- PrintElement(xmlFile.Root);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error: Could not read file from disk." + ex.Message);
- }
- }
- }
- // display an element (and its children, if any) in the TextBox
- private void PrintElement( XElement element )
- {
- // get element name without namespace
- string name = element.Name.LocalName;
- // display the element's name within its tag
- tbxOutput.AppendText( '<' + name + ">n" );
- // check for child elements and print value if none contained
- if ( element.HasElements )
- {
- // print all child elements at the next indentation level
- foreach ( var child in element.Elements() )
- // Display all attributes
- PrintElement(child);
- } // end if
- else
- {
- // display the text inside this element
- tbxOutput.AppendText( element.Value.Trim() + 'n' );
- } // end else
- // display end tag
- tbxOutput.AppendText( "</" + name + ">n" );
- } // end method PrintElement
- <xliff>
- <file>
- <body>
- <trans-unit>
- <source>
- Product Family
- </source>
- <target>
- Product Family
- </target>
- </trans-unit>
- </body>
- </file>
- </xliff>
- string GetAttributesString(XElement element)
- {
- if(element == null || !element.HasAttributes)
- return string.Empty;
- string format = " {0}={1}";
- StringBuider result = new StringBuilder();
- foreach(var attribute in element.Attributes())
- {
- result.AppendFormat(format, attribute.Name, attribute.Value);
- }
- return result.ToString();
- }
- tbxOutput.AppendText("<" + name + GetAttributesString(element) + ">n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement