Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <xliff version="1.2">
  3. <file source-language="en-US" datatype="plaintext" category="framework">
  4. <body>
  5. <transaction approved="no" id="1">
  6. <source xml:lang="en-US">Product Family</source>
  7. <target state="translated" xml:lang="en-US">Product Family</target>
  8. </transaction>
  9. </body>
  10. </file>
  11. </xliff>
  12.  
  13. private void btnOpen_Click(object sender, EventArgs e)
  14. {
  15. // Show the dialog, using defaults, and get result
  16. OpenFileDialog ofdResult = new OpenFileDialog();
  17.  
  18. if (ofdResult.ShowDialog() == DialogResult.OK)
  19. {
  20. try
  21. {
  22. if (ofdResult.OpenFile() != null)
  23. {
  24. XDocument xmlFile = XDocument.Load(ofdResult.FileName);
  25. // print elements recursively
  26. PrintElement(xmlFile.Root);
  27. }
  28. }
  29. catch (Exception ex)
  30. {
  31. MessageBox.Show("Error: Could not read file from disk." + ex.Message);
  32. }
  33. }
  34. }
  35.  
  36. // display an element (and its children, if any) in the TextBox
  37. private void PrintElement( XElement element )
  38. {
  39. // get element name without namespace
  40. string name = element.Name.LocalName;
  41.  
  42. // display the element's name within its tag
  43. tbxOutput.AppendText( '<' + name + ">n" );
  44.  
  45. // check for child elements and print value if none contained
  46. if ( element.HasElements )
  47. {
  48. // print all child elements at the next indentation level
  49. foreach ( var child in element.Elements() )
  50.  
  51. // Display all attributes
  52. PrintElement(child);
  53.  
  54. } // end if
  55. else
  56. {
  57. // display the text inside this element
  58. tbxOutput.AppendText( element.Value.Trim() + 'n' );
  59. } // end else
  60.  
  61. // display end tag
  62. tbxOutput.AppendText( "</" + name + ">n" );
  63. } // end method PrintElement
  64.  
  65. <xliff>
  66. <file>
  67. <body>
  68. <trans-unit>
  69. <source>
  70. Product Family
  71. </source>
  72. <target>
  73. Product Family
  74. </target>
  75. </trans-unit>
  76. </body>
  77. </file>
  78. </xliff>
  79.  
  80. string GetAttributesString(XElement element)
  81. {
  82. if(element == null || !element.HasAttributes)
  83. return string.Empty;
  84.  
  85. string format = " {0}={1}";
  86. StringBuider result = new StringBuilder();
  87. foreach(var attribute in element.Attributes())
  88. {
  89. result.AppendFormat(format, attribute.Name, attribute.Value);
  90. }
  91. return result.ToString();
  92. }
  93.  
  94. tbxOutput.AppendText("<" + name + GetAttributesString(element) + ">n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement