English 中文(简体)
C#中的XML
原标题:Parsing XML in C#

I am still working on a project and I am enjoying it greatly.

I wanted to see if I could implement a live updating feed using XML

当时,我甚至知道如何将这种特殊类型的XML混为一谈,因为我发现的所有辅导都是为了灌输 no的价值观等。

但我想到的是这方面的一些内容。

<Object name="ObjectName" type="ObjectType" size="ObjectSize" image="ObjectImage" />

如果说你的话,我能帮助我了解如何从那条 no子中获取一些内容,那将是令人惊讶的,如果仅作一个小小的解释,我就不明白了。 我知道,如何用XElement教导这种宗教。

<Object>
    <Name>ObjectName</Name>
    <Type>ObjectType</Type>
    <Size>ObjectSize</Size>
    <Image>ObjectImage</Image>
</Object>

I just cant seem to parse the example at the top, I dont mind if its Linq as long as it is in C#, maybe tell me why you would chose one over the other? Also have you got any idea on how to perhaps check if the file has changed, so I could implement a live update?

感谢您 帮助

John

最佳回答

The example at the high use attributes und sub-elements, but it s same srange to work with:

XElement element = XElement.Parse(xml);
string name = (string) element.Attribute("name");
string type = (string) element.Attribute("type");
string size = (string) element.Attribute("size");
string image = (string) element.Attribute("image");

我通常倾向于使用explicit string 2007,,而不是Value/code> 财产。 如您在<代码>null参考资料上进行转换,则您仅以null代替NullReferenceException。 当然,如果为遗嘱缺失而编制方案ror,那么例外情况更为合适,而 property则罚款。 (同样逻辑也适用于按行文转换<代码>XElement值。)

问题回答

如果你有代表你的文件的域名(通常情况),那么XmlSerializer就很容易使用。

[XmlRoot("Object")
public class Item
{
   public string Name { get; set; }
   public string Type { get; set; }
   public string Size { get; set; }
   public string Image { get; set; }
}

使用:

XmlSerializer ser = new XmlSerializer(typeof(Item));
Item item = (Item)ser.Deserialize(someXmlStream);

我认为,如果整个文件代表某种领域,采用这种办法比人工分类容易。

也可使用<代码>XEelment.FirstAttribute,以获得该要素的第一个属性,然后取到XAttribute。 下面的Attribute,以便通过它们。 这并不取决于你知道存在这种特性。

XAttribute attribute = element.FirstAttribute;
while (attribute != null)
{
    // Do stuff
    attribute = attribute.NextAttribute`
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签