Just to clarify, when I bring these in as import, I only care about the first reference. Also, if the reference tag is not there, I need to tell my code to skip it. SO the XML block would look like this with no ref URL:
when I bring these in as import, I only care about the first reference. Also, if the reference tag is not there, I need to tell my code to skip it.
According to your description, I suggest you could use
XmlNode.SelectNodes Method to check whether the current node contains the reference node. You could refer to the following code:
foreach (XmlNode node in nodeList)
{
//Use SelectNodes method to find reference node.
XmlNodeList typeNode = xn.SelectNodes(".//references");
//check whether current node contains the reference node
if (typeNode.Count>0)
{
string str = typeNode[0].InnerText;
}
else
{
//if it doesn't contains the reference node. skip this node.
continue;
}
// Access to the name ATTRIBUTE of the <cpe-item> tag:
Response.Write(String.Format("[{0:N0}] CPE: {1} Title: {2}", conta, node.Attributes["name"].Value, node.FirstChild.FirstChild.Value));
// Split out the InnerText for Manufacturer, Model, and Version
// If version is empty, use - as place holder
// Access to the <title> tag content:
//Debug.WriteLine(String.Format("[{0:N0}] Title: {1} Title: {2}", conta, node.SelectSingleNode("./title", nsmgr)));
XmlNode titleNode = node.SelectSingleNode("./title", nsmgr);
// splitting out values
string s = node.Attributes["name"].Value;
s = s.Replace("/", "");
var array = s.Split(':');
cpetype = s[1].ToString();
manuf = s[2].ToString();
prod = s[3].ToString();
vers = s[4].ToString();
cpe = "cpe:2.3:" + cpetype.ToString() + ":" + manuf.ToString() + ":" + prod.ToString() + ":" + vers.ToString() + "";
}
Best Regards,
Dillion
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Since my previous sample, I haven't added the namespace property, so you can't find the relevant node.
Please try to use the following code, I tested on my side, it worked well.
protected void btnUpload_Click(object sender, EventArgs e)
{
string cpetype = string.Empty, manuf = string.Empty, prod = string.Empty, vers = string.Empty, cpe = string.Empty;
XmlDocument myDoc = new XmlDocument();
myDoc.Load(FileUpload1.FileContent);
// Add the namespaces:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(myDoc.NameTable);
nsmgr.AddNamespace("ns6", "http://scap.nist.gov/schema/scap-core/0.1");
nsmgr.AddNamespace("cpe-23", "http://scap.nist.gov/schema/cpe-extension/2.3");
nsmgr.AddNamespace("ns", "http://cpe.mitre.org/dictionary/2.0");
nsmgr.AddNamespace("meta", "http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2");
nsmgr.AddNamespace("scap-core", "http://scap.nist.gov/schema/scap-core/0.3");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("config", "http://scap.nist.gov/schema/configuration/0.1");
XmlNodeList nodeList;
nodeList = myDoc.DocumentElement.SelectNodes("//ns:cpe-list/ns:cpe-item", nsmgr);
long conta = 0;
StringBuilder sb = new StringBuilder();
foreach (XmlNode node in nodeList)
{
//Use SelectNodes method to find reference node.
XmlNodeList typeNode = node.SelectNodes(".//ns:references",nsmgr);
//check whether current node contains the reference node
if (typeNode.Count > 0)
{
string str = typeNode[0].InnerText;
}
else
{
//if it doesn't contains the reference node. skip this node.
continue;
}
// Access to the name ATTRIBUTE of the <cpe-item> tag:
sb.AppendLine(String.Format("[{0:N0}] CPE: {1} Title: {2}", conta, node.Attributes["name"].Value, node.FirstChild.FirstChild.Value));
}
Response.Write(sb.ToString());
}
Best regards,
Dillion
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Ok, I changed the code to typeNode[0].InnerXml, this gave me the URL of the vendor site but it also gave me several other things in the string builder.
How would you go about grabbing just the first reference href and drop the rest? The only part out of the string I need is the first URL.
As for this issue, you could use String.Substring method to get the first URL. Please refer to the following code:
//Use SelectNodes method to find reference node.
XmlNodeList typeNode = node.SelectNodes("./ns:references",nsmgr);
//check whether current node contains the reference node
if (typeNode.Count > 0)
{
string str = typeNode[0].InnerText;
string str2 = typeNode[0].InnerXml.ToString(); //output: <reference href="http://12net.jp" xmlns="http://cpe.mitre.org/dictionary/2.0">vendor website</reference><reference href="http://wordpress.org/plugins/login-rebuilder/changelog/" xmlns="http://cpe.mitre.org/dictionary/2.0">product changelog</reference>
int startindex = str2.IndexOf("href");
int endIndex = str2.IndexOf("xmlns");
string url = str2.Substring(startindex + 6, endIndex - startindex - 8); //output: http://12net.jp
}
else
{
//if it doesn't contains the reference node. skip this node.
continue;
}
Besides, I suppose you could also try to use
Regex.Match Method to get the first URL.
Best regards,
Dillion
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
35169 Points
9930 Posts
Moderator
Need to pull out vendor reference info from XML
Aug 31, 2015 12:53 PM|bbcompent1|LINK
Guys, I am stuck. I need to bring the reference URL in if there is one. That XML looks like this:
Just to clarify, when I bring these in as import, I only care about the first reference. Also, if the reference tag is not there, I need to tell my code to skip it. SO the XML block would look like this with no ref URL:
I am doing this with C#.net. Currently, I read the data using this code:
All-Star
45439 Points
7008 Posts
Microsoft
Re: Need to pull out vendor reference info from XML
Sep 01, 2015 10:02 PM|Zhi Lv - MSFT|LINK
Hi bbcompent1,
According to your description, I suggest you could use XmlNode.SelectNodes Method to check whether the current node contains the reference node. You could refer to the following code:
foreach (XmlNode node in nodeList) { //Use SelectNodes method to find reference node. XmlNodeList typeNode = xn.SelectNodes(".//references"); //check whether current node contains the reference node if (typeNode.Count>0) { string str = typeNode[0].InnerText; } else { //if it doesn't contains the reference node. skip this node. continue; } // Access to the name ATTRIBUTE of the <cpe-item> tag: Response.Write(String.Format("[{0:N0}] CPE: {1} Title: {2}", conta, node.Attributes["name"].Value, node.FirstChild.FirstChild.Value)); // Split out the InnerText for Manufacturer, Model, and Version // If version is empty, use - as place holder // Access to the <title> tag content: //Debug.WriteLine(String.Format("[{0:N0}] Title: {1} Title: {2}", conta, node.SelectSingleNode("./title", nsmgr))); XmlNode titleNode = node.SelectSingleNode("./title", nsmgr); // splitting out values string s = node.Attributes["name"].Value; s = s.Replace("/", ""); var array = s.Split(':'); cpetype = s[1].ToString(); manuf = s[2].ToString(); prod = s[3].ToString(); vers = s[4].ToString(); cpe = "cpe:2.3:" + cpetype.ToString() + ":" + manuf.ToString() + ":" + prod.ToString() + ":" + vers.ToString() + ""; }
Best Regards,
Dillion
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
35169 Points
9930 Posts
Moderator
Re: Need to pull out vendor reference info from XML
Sep 02, 2015 09:02 AM|bbcompent1|LINK
Dillon, typeNode.Count is always 0. For your reference, this is the data file:
and here is my complete source code:
All-Star
35169 Points
9930 Posts
Moderator
Re: Need to pull out vendor reference info from XML
Sep 03, 2015 08:44 AM|bbcompent1|LINK
I am still working on this but I seem to be getting nowhere fast. Here is the code now:
All-Star
45439 Points
7008 Posts
Microsoft
Re: Need to pull out vendor reference info from XML
Sep 03, 2015 09:36 PM|Zhi Lv - MSFT|LINK
Hi bbcompent1,
Since my previous sample, I haven't added the namespace property, so you can't find the relevant node.
Please try to use the following code, I tested on my side, it worked well.
protected void btnUpload_Click(object sender, EventArgs e) { string cpetype = string.Empty, manuf = string.Empty, prod = string.Empty, vers = string.Empty, cpe = string.Empty; XmlDocument myDoc = new XmlDocument(); myDoc.Load(FileUpload1.FileContent); // Add the namespaces: XmlNamespaceManager nsmgr = new XmlNamespaceManager(myDoc.NameTable); nsmgr.AddNamespace("ns6", "http://scap.nist.gov/schema/scap-core/0.1"); nsmgr.AddNamespace("cpe-23", "http://scap.nist.gov/schema/cpe-extension/2.3"); nsmgr.AddNamespace("ns", "http://cpe.mitre.org/dictionary/2.0"); nsmgr.AddNamespace("meta", "http://scap.nist.gov/schema/cpe-dictionary-metadata/0.2"); nsmgr.AddNamespace("scap-core", "http://scap.nist.gov/schema/scap-core/0.3"); nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); nsmgr.AddNamespace("config", "http://scap.nist.gov/schema/configuration/0.1"); XmlNodeList nodeList; nodeList = myDoc.DocumentElement.SelectNodes("//ns:cpe-list/ns:cpe-item", nsmgr); long conta = 0; StringBuilder sb = new StringBuilder(); foreach (XmlNode node in nodeList) { //Use SelectNodes method to find reference node. XmlNodeList typeNode = node.SelectNodes(".//ns:references",nsmgr); //check whether current node contains the reference node if (typeNode.Count > 0) { string str = typeNode[0].InnerText; } else { //if it doesn't contains the reference node. skip this node. continue; } // Access to the name ATTRIBUTE of the <cpe-item> tag: sb.AppendLine(String.Format("[{0:N0}] CPE: {1} Title: {2}", conta, node.Attributes["name"].Value, node.FirstChild.FirstChild.Value)); } Response.Write(sb.ToString()); }
Best regards,
Dillion
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
35169 Points
9930 Posts
Moderator
Re: Need to pull out vendor reference info from XML
Sep 04, 2015 10:07 AM|bbcompent1|LINK
Thanks Dillon, I will give this a try!
All-Star
35169 Points
9930 Posts
Moderator
Re: Need to pull out vendor reference info from XML
Sep 04, 2015 10:14 AM|bbcompent1|LINK
Ok, I changed the code to typeNode[0].InnerXml, this gave me the URL of the vendor site but it also gave me several other things in the string builder.
<reference href="http://12net.jp" xmlns="http://cpe.mitre.org/dictionary/2.0">vendor website</reference><reference href="http://wordpress.org/plugins/login-rebuilder/changelog/" xmlns="http://cpe.mitre.org/dictionary/2.0">product changelog</reference>
How would you go about grabbing just the first reference href and drop the rest? The only part out of the string I need is the first URL.
All-Star
45439 Points
7008 Posts
Microsoft
Re: Need to pull out vendor reference info from XML
Sep 08, 2015 05:09 AM|Zhi Lv - MSFT|LINK
Hi bbcompent1,
As for this issue, you could use String.Substring method to get the first URL. Please refer to the following code:
Besides, I suppose you could also try to use Regex.Match Method to get the first URL.
Best regards,
Dillion
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.