|
You need to use a XmlNamespaceManager .
string xml =
@"<?xml version=""1.0"" encoding=""UTF-8""?>
<session xmlns:ns1=""foo"" xmlns:ns2=""bar"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:noNamespaceSchemaLocation=""baz.xsd"">
<gateway>
<ns1:path></ns1:path>
<ns1:file>""gateway.xml""</ns1:file>
</gateway>
<strats>
<ns1:path></ns1:path>
<ns1:file>""strats.xml""</ns1:file>
</strats>
<log>
<ns1:path></ns1:path>
<ns1:file>""session.log""</ns1:file>
</log>
</session>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNamespaceManager nm = new XmlNamespaceManager(doc.NameTable);
nm.AddNamespace("ns1", "foo");
XmlNode node = doc.SelectSingleNode("session/gateway/ns1:file", nm);
if(node != null)
Console.WriteLine(node.OuterXml);
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
So this means my C# code guts must know the namespace and namespace aliases used in the xml schema? You write
nm.AddNamespace("ns1", "foo");
Isn't this fragile to schema changes?
Does XmlSchema allow for importing my schema, against which the xml doc was created, so that the namespace and namespace aliases are imported?
-- modified at 8:46 Wednesday 15th February, 2006
|
|
|
|
|
Your C# code must know the namespaces. The namespace prefixes are immaterial. The code that I submitted to you would work just as well if I made the following changes.
nm.AddNamespace("goobledygoobledygook", "foo");
XmlNode node = doc.SelectSingleNode("session/gateway/goobledygoobledygook:file", nm);
if(node != null)
Console.WriteLine(node.OuterXml); The prefixes are aliases, as you point out. However, to get to a node that belongs in another namespace, you will have to know to which namespace that node belongs. The file node, for example, lives in another namespace. Because of that, your C# code (and you!) will have to handle that.
If you don't want to deal with the namespaces, use a regular expression to remove all prefixes from the string representation (i.e., XML source) and put that into an XmlDocument .
|
|
|
|
|
Curtis, thank you for your time on this. Perhaps you can respond to the larger question. I have XMLSpy and it is natural (to me) to build reusable schemas which I 'type' (including a namespace) and then import into a larger schema. Then I build xml doc's against this schema.
On the C# side ideally I'd just like to traverse the node tree. So I've learned that either I keep a namespace table to read the hierarchical xml docs, or I flatten the schemas into 1 level, or I use your suggestion of cleaning the doc using regex.
What do you consider 'best practices' here?
Thank you.
|
|
|
|
|
That's a tough one. If you use XPath, then you'll have to perform one of the three. In that case, I would recommend using the method that would present the simplest solution to you. The three, in practice, have a common outcome; thus, you should choose the one with which you feel most comfortable.
I hope that input helps.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
hi
how can i get or make an Icon for execute(exe) file in my project?
thanks !
s_mostafa_h
|
|
|
|
|
Add the icon to your exe project. Select the icon file in your tree view and show its properties. For the "Content Type" property select "Embedded Resource". Now, select your exe project in the tree view and show properties. Under the General tab there is an "Application Icon" property. Set this property to the file name of your icon.
I hope this helps.
Deus caritas est
|
|
|
|
|
When I try this it says that my ico file is bad, what are the properties that it requires the icon to be? I read it was picky, but the book had no data on it.
|
|
|
|
|
Try a 16x16 icon with 256 colors. That is what I am using. I suppose a 24-bit color icon would work too but I think it has to be 16x16.
Deus caritas est
|
|
|
|
|
THANKS FOR YOUR Reply !
s_mostafa_h
|
|
|
|
|
How can you extract the metadata or summary or tags of a PDF file in c#.
For example: Author, Title, etc.
-- modified at 16:42 Tuesday 14th February, 2006
|
|
|
|
|
Is there a way in C# using win32 to arrange the child windows of ReBarWindow32 (IE) like ComboBoxEx32, ToolbarWindow32 etc in same line. My purpose is to programmatically set a single line for the explorer toolbar section so that menu, tools and addressbar should be horizontally aligned.
Any help in this regard would be highly appreciated ..
thanks
Sree..
|
|
|
|
|
I hope I can explain this without sounding a complete idiot.
I have an application which has a number of classes which handle the business logic of the app and a number of forms which handle the GUI. What I want to do is use .NET remoting to have the business logic handled on a server while the clients just worry about displaying information to the user.
All of the examples I've found so far have displayed moving a simple class to a server and the server explicitly names the class when it starts up. What I need to know is how to use a single server application to distribute all of the neccessary classes for the overall application. This would essencially require the clients to be able to create instances of the remote classes and use their methods and properties.
If I'm being really thick or I seem to be trying to do something stupid then please let me know but if anyones got any ideas which might help then please let me know
Martin Keeton
|
|
|
|
|
mk20 wrote: clients to be able to create instances of the remote classes and use their methods and properties.
If this was not the case, then webservices would be an excellent choice. One thing you can do, is expose all the public instance methods as public static methods throuh webservice and pass the object on which to call the method as the parameter to the static method.
You could use remoting, but if you have multiple versions of the program, things may go out of control easily.
Using webservice:
<code>public class Employee
{
public void PromoteAsSupervisor()
{
...
}
public void GiveRaise(int value)
{
...
}
public void AssignProject(Project p, Duration d)
{
...
}
}
public class MyWebService:WebService
{
[WebMethod()]
public void PromoteAsSupervisor (Employee employee, params object[] parameters)
{
if (employee==null)
{
throw new ArguementNullException("employee");
}
else
{
employee.PromoteAsSupervisor();
}
}
[WebMethod()]
public void GiveRaise (Employee employee, params object[] parameters)
{
if (employee==null)
{
throw new ArguementNullException("employee");
}
else
{
employee.GiveRaise((int) parameters[0]);
}
}
[WebMethod()]
public void AssignProject (Employee employee, Project p, Duration d)
{
if (employee==null)
{
throw new ArguementNullException("employee");
}
else
{
employee.AssignProject(p, d);
}
}
[WebMethod()]
public void InvokeMethod (object obj, string method, params object[] parameters)
{
}
}</code>
- Malhar
|
|
|
|
|
I wanted to try and create a new DataGridViewColumnHeaderCell class that would allow me the option of having the column headers displayed vertically instead or horizontally. So I created a new class that inherits from DataGridViewColumnHeaderCell and overrode the Paint and GetPreferredSize methods. This seems to work okay so long as all my columns are visible from the start (except I can't see the result at design time, which is annoying). The problem is when I start with a column that is not visible and then make it visible later so weird things happen. The columns are set to autofit their contents but the width and height aren't getting set correctly. Here's my override for GetPreferredSize
protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
{
if (!verticalText)
{
return base.GetPreferredSize(graphics, cellStyle, rowIndex, constraintSize);
}
else
{
StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical);
SizeF size = graphics.MeasureString((string)this.FormattedValue, this.DataGridView.Font, constraintSize, sf);
return new Size((int)size.Width, (int)size.Height);
}
}
Is there something I'm missing here? Is there something else I need to override? Has anybody else tried something similar?
I would at least like the width to be set correctly as I can just manually set the height of the header row to something sufficient.
Thanks
|
|
|
|
|
I'm using Reflection to extract Property/Method/Event/Field information from an assembly. Assuming that I have an object of type [Property|Method|Event|Field]Info , how do I know if the implementation is derived from the base class or if it has been overriden? I need to know how to make this distinction. I tried various BindingFlags option but no luck yet.
In the following example, when I retreive all Properties for Supervisor class, I only want "EmployeesUnderMe" returned in the collection. I should not have "Salary" and "Bonus". I SHOULD have "Bonus" if Supervisor class overrode the property. Similarly, when I invoke "GetMethods(...)" on Supervisor class, I want to be ablel to distinguish that "ToString()", "GetHashCode()" etc are defined by the "object" class and NOT the Supervisor class.
<code>
public class Employee
{
public double Salary
{
get {...}
set {...}
}
public virtual Bonus
{
get {...}
set {...}
}
public Employee()
{
...
}
}
public class Supervisor:Employee
{
public Employeee[] EmployeesUnderMe
{
get {...}
}
public Supervisor()
{
}
}
</code>
Thanks for your help.
- Malhar
|
|
|
|
|
malharone wrote: I tried various BindingFlags option but no luck yet.
BindingFlags.Declared only
I think you can also try see if the methodinfo's declaredtype and reflectedtype is the same.
|
|
|
|
|
Thank you .. that did the trick. It is much better to know the project has ~7,000 methods as opposed to 150,000 methods!
- Malhar
|
|
|
|
|
Hi,
I have to pass a native Struct from my C++ DLL into my C# program.
The problem is that the struct contains a C++ "union" member.
Can someone recommend a good way to marshal the struct into my managed code?
struct NATIVE_STRUCT
{
DWORD Value1;
DWORD Value2;
union
{
STRUCT1 Member1;
STRUCT2 Member2;
} UnionMember;
};
Thanks a lot,
Rich
|
|
|
|
|
Nevermind...I found the answer.
You have to declare the union member as a struct and use various attributes to describe the physical layout of the struct, such as the [FieldOffset()] attribute.
|
|
|
|
|
Troposphere wrote: I found the answer
There are various tricks you can use in marshalling, just as long as the layout stays the same
|
|
|
|
|
I have a C# Windows Forms app, and I want to launch a whole separate application (another .exe file) when the user clicks a button.
What class and method do I use? It should work just like Start -> Run on the Windows Task Bar.
Thanks,
Mark
|
|
|
|
|
You can use the Process class (part of the System.Diagnostics namespace)
using System.Diagnostics;
...
Process.Start("<path to .exe>");
Cheers,
Will H
|
|
|
|
|
I have this number from which I have to read first few number depending on another number. What is the best way to do that.
For example ... NumToread = 3 , number = "4567889465"
answer should newNum = 456
i can do that using for (int i=1;i<=NumToread ;i++)
does any one knows better way ...
As you can say .. I am very new to programming
Thanks
CPP
Thanks
|
|
|
|
|
You should use the String.Substring() method.
int numToRead = 3;<br />
string buf = "123456789";<br />
buf = buf.Substring(0, 3);
|
|
|
|