1. 程式人生 > 程式設計 >c#如何使用 XML 文件功能

c#如何使用 XML 文件功能

下面的示例提供對某個已存檔型別的基本概述。

示例

// If compiling from the command line,compile with: -doc:YourFileName.xml

/// <summary>
/// Class level summary documentation goes here.
/// </summary>
/// <remarks>
/// Longer comments can be associated with a type or member through
/// the remarks tag.
/// </remarks>
public class TestClass : TestInterface
{
  /// <summary>
  /// Store for the Name property.
  /// </summary>
  private string _name = null;

  /// <summary>
  /// The class constructor.
  /// </summary>
  public TestClass()
  {
    // TODO: Add Constructor Logic here.
  }

  /// <summary>
  /// Name property.
  /// </summary>
  /// <value>
  /// A value tag is used to describe the property value.
  /// </value>
  public string Name
  {
    get
    {
      if (_name == null)
      {
        throw new System.Exception("Name is null");
      }
      return _name;
    }
  }

  /// <summary>
  /// Description for SomeMethod.
  /// </summary>
  /// <param name="s"> Parameter description for s goes here.</param>
  /// <seealso cref="System.String">
  /// You can use the cref attribute on any tag to reference a type or member
  /// and the compiler will check that the reference exists.
  /// </seealso>
  public void SomeMethod(string s)
  {
  }

  /// <summary>
  /// Some other method.
  /// </summary>
  /// <returns>
  /// Return values are described through the returns tag.
  /// </returns>
  /// <seealso cref="SomeMethod(string)">
  /// Notice the use of the cref attribute to reference a specific method.
  /// </seealso>
  public int SomeOtherMethod()
  {
    return 0;
  }

  public int InterfaceMethod(int n)
  {
    return n * n;
  }

  /// <summary>
  /// The entry point for the application.
  /// </summary>
  /// <param name="args"> A list of command line arguments.</param>
  static int Main(System.String[] args)
  {
    // TODO: Add code to start application here.
    return 0;
  }
}

/// <summary>
/// Documentation that describes the interface goes here.
/// </summary>
/// <remarks>
/// Details about the interface go here.
/// </remarks>
interface TestInterface
{
  /// <summary>
  /// Documentation that describes the method goes here.
  /// </summary>
  /// <param name="n">
  /// Parameter n requires an integer argument.
  /// </param>
  /// <returns>
  /// The method returns an integer.
  /// </returns>
  int InterfaceMethod(int n);
}

該示例生成一個包含以下內容的 .xml 檔案。

<?xml version="1.0"?>
<doc>
  <assembly>
    <name>xmlsample</name>
  </assembly>
  <members>
    <member name="T:TestClass">
      <summary>
      Class level summary documentation goes here.
      </summary>
      <remarks>
      Longer comments can be associated with a type or member through
      the remarks tag.
      </remarks>
    </member>
    <member name="F:TestClass._name">
      <summary>
      Store for the Name property.
      </summary>
    </member>
    <member name="M:TestClass.#ctor">
      <summary>
      The class constructor.
      </summary>
    </member>
    <member name="P:TestClass.Name">
      <summary>
      Name property.
      </summary>
      <value>
      A value tag is used to describe the property value.
      </value>
    </member>
    <member name="M:TestClass.SomeMethod(System.String)">
      <summary>
      Description for SomeMethod.
      </summary>
      <param name="s"> Parameter description for s goes here.</param>
      <seealso cref="T:System.String">
      You can use the cref attribute on any tag to reference a type or member
      and the compiler will check that the reference exists.
      </seealso>
    </member>
    <member name="M:TestClass.SomeOtherMethod">
      <summary>
      Some other method.
      </summary>
      <returns>
      Return values are described through the returns tag.
      </returns>
      <seealso cref="M:TestClass.SomeMethod(System.String)">
      Notice the use of the cref attribute to reference a specific method.
      </seealso>
    </member>
    <member name="M:TestClass.Main(System.String[])">
      <summary>
      The entry point for the application.
      </summary>
      <param name="args"> A list of command line arguments.</param>
    </member>
    <member name="T:TestInterface">
      <summary>
      Documentation that describes the interface goes here.
      </summary>
      <remarks>
      Details about the interface go here.
      </remarks>
    </member>
    <member name="M:TestInterface.InterfaceMethod(System.Int32)">
      <summary>
      Documentation that describes the method goes here.
      </summary>
      <param name="n">
      Parameter n requires an integer argument.
      </param>
      <returns>
      The method returns an integer.
      </returns>
    </member>
  </members>
</doc>

編譯程式碼

若要編譯該示例,請輸入以下命令:

csc XMLsample.cs /doc:XMLsample.xml

此命令建立 XML 檔案 XMLsample.xml,可在瀏覽器中或使用 TYPE 命令檢視該檔案。

可靠程式設計

XML 文件以 /// 開頭。 建立新專案時,嚮導會放置一些以 /// 開頭的行。 處理這些註釋時存在一些限制:

1.文件必須是格式正確的 XML。 如果 XML 格式不正確,則會生成警告,並且文件檔案將包含一條註釋,指出遇到錯誤。

2.開發人員可以隨意建立自己的標記集。 有一組推薦的標記。 部分建議標記具有特殊含義:

  • <param> 標記用於描述引數。 如果已使用,編譯器會驗證該引數是否存在,以及文件是否描述了所有引數。 如果驗證失敗,編譯器會發出警告。
  • cref 屬性可以附加到任何標記,以引用程式碼元素。 編譯器驗證此程式碼元素是否存在。 如果驗證失敗,編譯器會發出警告。 編譯器在查詢 cref 屬性中描述的型別時會考慮所有 using 語句。
  • <summary> 標記由 Visual Studio 中的 IntelliSense 用於顯示有關某個型別或成員的附加資訊。

備註

XML 檔案不提供有關該型別和成員的完整資訊(例如,它不包含任何型別資訊)。 若要獲取有關型別或成員的完整資訊,請將文件檔案與對實際型別或成員的反射一起使用。

以上就是c#如何使用 XML 文件功能的詳細內容,更多關於c# 使用 XML 文件功能的資料請關注我們其它相關文章!