C# 如何在C中解析和提取XML节点描述中的值#

C# 如何在C中解析和提取XML节点描述中的值#,c#,xml,C#,Xml,我能够解析和提取xml标记中的内部文本。我无法提取作业标签中的值 我的Xml文件格式 <?xml version="1.0" standalone="yes"?> <SmPpProperties> <SmProperties> <Job Name="Job001" CreatedDate="2012-10-15T10:43:56.0972966-06:00" ModifiedDate="2012-10-

我能够解析和提取xml标记中的内部文本。我无法提取作业标签中的值
我的Xml文件格式

    <?xml version="1.0" standalone="yes"?>
    <SmPpProperties>
     <SmProperties>
       <Job Name="Job001" CreatedDate="2012-10-15T10:43:56.0972966-06:00" ModifiedDate="2012-10-       15T10:46:07.6878231-06:00">
 //            **I am not able to extract the values present in above Job tag**
         <NuclearSystem>Barium</NuclearSystem>
                      </Job>
    </SmProperties>
 <SmPpProperties>
返回节点及其子注释的连接值。这将为您提供
钡剂
。名称、ModifiedDate和CreatedDate是属性

您的意图不清楚,但如果您希望获得所有属性的串联值:

String.Join(",", MyNode.Attributes.Cast<XmlAttribute>().Select(a => a.Value))
注意:我始终建议您使用Linq to Xml解析Xml。您可以轻松地从xml创建强类型对象:

var xdoc = XDocument.Load(@"C:\Users\SRangarajan\Desktop\12001_.xml");
XElement j = xdoc.Root.Element("SmProperties").Element("Job");
var job = new {
                Name = (string)j.Attribute("Name"),
                CreatedDate = (DateTime)j.Attribute("CreatedDate"),
                ModifiedDate = (DateTime)j.Attribute("ModifiedDate"),
                NuclearSystem = (string)j.Element("NuclearSystem")
            };
这将为您提供作业对象,该对象将具有名称、创建日期和修改日期的强类型属性。日期属性将具有
DateTime
类型


尝试使用linq,如下所示:

string xml = "xml";
    XDocument xdoc = XDocument.Parse(xml);
    XElement nuclear = xdoc.Descendants(document.Root.Name.Namespace + "NuclearSystem").FirstOrDefault();

    string nuclearSystem = nuclear.Value();

要获取属性,请使用Attributes属性,然后访问值:

XmlNode MyNode = MyDoc.SelectSingleNode("SmPpProperties/SmProperties/Job");
Console.WriteLine(String.Concat("Name: ", MyNode.Attributes["Name"].Value));
Console.WriteLine(String.Concat("CreatedDate: ", MyNode.Attributes["CreatedDate"].Value));
Console.WriteLine(String.Concat("ModifiedDate: ", MyNode.Attributes["ModifiedDate"].Value));
Console.WriteLine(String.Concat("NuclearSystem: ", MyNode.InnerText));

@谢尔盖:谢谢你的迅速回复,我应该这样试试吗?字符串Job=node.Attributes[3]。值@ShrivatsanRangarajan是的,您应该使用
XmlNode.Attributes
属性。您可以按索引、按名称获取属性,也可以按我在答案中显示的方式获取所有属性。是的,
xmlatrubute
具有value属性,该属性返回其值@Sergey:Thumbs作为您的替代解决方案。谢谢。我还需要处理这些属性的数据类型转换,以便插入到我的sql数据库中table@ShrivatsanRangarajan对不起,我没听懂你的话。什么数据类型转换?我的Sql表模式-创建表活动(Nuclear_System CHAR(20))需要将提取的数据插入该表。我可以建立的连接字符串…DB数据类型inputPerfect如何回答。谢谢。我正要就此提出一个问题。谢谢你的回复。
string xml = "xml";
    XDocument xdoc = XDocument.Parse(xml);
    XElement nuclear = xdoc.Descendants(document.Root.Name.Namespace + "NuclearSystem").FirstOrDefault();

    string nuclearSystem = nuclear.Value();
XmlNode MyNode = MyDoc.SelectSingleNode("SmPpProperties/SmProperties/Job");
Console.WriteLine(String.Concat("Name: ", MyNode.Attributes["Name"].Value));
Console.WriteLine(String.Concat("CreatedDate: ", MyNode.Attributes["CreatedDate"].Value));
Console.WriteLine(String.Concat("ModifiedDate: ", MyNode.Attributes["ModifiedDate"].Value));
Console.WriteLine(String.Concat("NuclearSystem: ", MyNode.InnerText));