2020年11月30日 星期一

使用別的namespace並且更改build order (轉貼)

 

Asked 
Viewed 34k times
34

I have two projects within a single solution in Visual Studio 2010. These projects are called Project1 and Project2. Within these projects, two namespaces are defined, Namespace1 and Namespace2, respectively.

Inside some code within Namespace2, I'd like to use some structs, classes, etc. which I've defined in Namespace1. Is there any way to do this?

57

Yes, add a reference to Project1 from Project2. Right-click the project, choose "Add References" then from the "Projects" tab, choose Project1.









Set Project Build Order in Visual Studio

In order to set the Build order for your Solution, right-click on the Solution in Solution Explorer and Select Project Build Order:

Your Project Dependencies should be set correctly which is used to determine the Build order by Visual Studio.

The image below shows the Project references added in the Business layer to determine that the DTO and Persistence Projects should be built first before the Business layer Project.

The Project Build order will make sure the required dlls are available for the Api to compile correctly.

2020年11月26日 星期四

XElement用法(轉貼)

 參:https://docs.microsoft.com/zh-tw/dotnet/api/system.xml.linq.xcontainer.add?view=net-5.0

範例

下列範例會建立兩個 XML 樹狀結構,然後使用這個方法,將查詢的結果加入其中一個。

C#
XElement srcTree = new XElement("Root",  
    new XElement("Element1", 1),  
    new XElement("Element2", 2),  
    new XElement("Element3", 3),  
    new XElement("Element4", 4),  
    new XElement("Element5", 5)  
);  
XElement xmlTree = new XElement("Root",  
    new XElement("NewElement", "Content")  
);  
xmlTree.Add(  
    from el in srcTree.Elements()  
    where (int)el >= 3  
    select el  
);  
Console.WriteLine(xmlTree);  

這個範例會產生下列輸出:

XML
<Root>  
  <NewElement>Content</NewElement>  
  <Element3>3</Element3>  
  <Element4>4</Element4>  
  <Element5>5</Element5>  
</Root>  

2020年11月25日 星期三

Control item in this.Controls.OfType<..>() 可能不會列出所有加到Controls的item

如下code:

 foreach (Control item in this.Controls.OfType<CheckBox>())

    {
        this.Controls.Remove(item); 
    }
會有一些成員不會刪除,我這台電腦發生的是,3, 7...不會刪除。
解法是,直接把要刪掉的成員放進去Remove:
foreach(var item in cbox)
            {
                this.Controls.Remove(item);
            }

Iterating through all nodes in XML file (轉貼)

 

Asked 
Viewed 167k times
36

I want to iterate through all nodes in an XML file and print their names. What is the best way to do this? I am using .NET 2.0.

38

I think the fastest and simplest way would be to use an XmlReader, this will not require any recursion and minimal memory foot print.

Here is a simple example, for compactness I just used a simple string of course you can use a stream from a file etc.

  string xml = @"
    <parent>
      <child>
        <nested />
      </child>
      <child>
        <other>
        </other>
      </child>
    </parent>
    ";

  XmlReader rdr = XmlReader.Create(new System.IO.StringReader(xml));
  while (rdr.Read())
  {
    if (rdr.NodeType == XmlNodeType.Element)
    {
      Console.WriteLine(rdr.LocalName);
    }
  }

The result of the above will be

parent
child
nested
child
other

A list of all the elements in the XML document.